How to Check If a String Contains Another String in Objective-C

How do I check if a string contains another string in Objective-C?

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
NSLog(@"string does not contain bla");
} else {
NSLog(@"string contains bla!");
}

The key is noticing that rangeOfString: returns an NSRange struct, and the documentation says that it returns the struct {NSNotFound, 0} if the "haystack" does not contain the "needle".


And if you're on iOS 8 or OS X Yosemite, you can now do: (*NOTE: This WILL crash your app if this code is called on an iOS7 device).

NSString *string = @"hello bla blah";
if ([string containsString:@"bla"]) {
NSLog(@"string contains bla!");
} else {
NSLog(@"string does not contain bla");
}

(This is also how it would work in Swift)

How do I check if a string contains another string in Swift?

You can do exactly the same call with Swift:

Swift 4 & Swift 5

In Swift 4 String is a collection of Character values, it wasn't like this in Swift 2 and 3, so you can use this more concise code1:

let string = "hello Swift"
if string.contains("Swift") {
print("exists")
}

Swift 3.0+

var string = "hello Swift"

if string.range(of:"Swift") != nil {
print("exists")
}

// alternative: not case sensitive
if string.lowercased().range(of:"swift") != nil {
print("exists")
}

Older Swift

var string = "hello Swift"

if string.rangeOfString("Swift") != nil{
println("exists")
}

// alternative: not case sensitive
if string.lowercaseString.rangeOfString("swift") != nil {
println("exists")
}

I hope this is a helpful solution since some people, including me, encountered some strange problems by calling containsString().1

PS. Don't forget to import Foundation

Footnotes

  1. Just remember that using collection functions on Strings has some edge cases which can give you unexpected results, e. g. when dealing with emojis or other grapheme clusters like accented letters.

how to see if a string contains a substring (objective-c)

"then when i scroll back up all the recycled cells have the steve jobs image"

That's because you're not resetting the image in the recycled cells. (Try adding

else { 
cell.imageView.image = nil;
}

at an appropriate place.)

What Objective-C NSString method need to check if a string is contained in another?

I assume that you mean you're using localizedCaseInsensitiveContainsString: since the method you've cited doesn't do what you're saying.

There's a corresponding method to get the range of the search term: -[NSString localizedStandardRangeOfString:] You use the returned range to index back into the source string.

You can also use the rangeOfString:options:range:locale: if you need to search with a locale other than the current one.

Check IF one String contains the same characters as another string

I think this method could be rather slow, but I would still favour it over [NSString rangeOfCharacterFromSet:], which requires creating an NSCharacterSet object per comparison:

- (BOOL)string:(NSString *)string containsAllCharactersInString:(NSString *)charString {
NSUInteger stringLen = [string length];
NSUInteger charStringLen = [charString length];
for (NSUInteger i = 0; i < charStringLen; i++) {
unichar c = [charString characterAtIndex:i];
BOOL found = NO;
for (NSUInteger j = 0; j < stringLen && !found; j++)
found = [string characterAtIndex:j] == c;
if (!found)
return NO;
}
return YES;
}

How to search a string with space within another string in objective-C?

I don't know if i understand wich you want to do. But if you want to check if string contains in another string you need to do this:

NSString *yourString = @"Data your string";
if ([yourString rangeOfString:@"Data "].location == NSNotFound) {
NSLog(@"not contains Data ");
} else {
NSLog(@"contains Data ");
}


Related Topics



Leave a reply



Submit