How do I check if a string contains another string in Swift?
Asked 07 September, 2021
Viewed 2.7K times
  • 56
Votes

In Objective-C the code to check for a substring in an NSString is:

NSString *string = @"hello Swift";
NSRange textRange =[string rangeOfString:@"Swift"];
if(textRange.location != NSNotFound)
{
    NSLog(@"exists");
}

But how do I do this in Swift?

27 Answer