0Day Forums
How do I check if a string contains another string in Objective-C? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Objective-C (https://0day.red/Forum-Objective-C)
+--- Thread: How do I check if a string contains another string in Objective-C? (/Thread-How-do-I-check-if-a-string-contains-another-string-in-Objective-C)

Pages: 1 2 3


How do I check if a string contains another string in Objective-C? - elinejevtt - 07-21-2023

How can I check if a string (`NSString`) contains another smaller string?

I was hoping for something like:

NSString *string = @"hello bla bla";
NSLog(@"%d",[string containsSubstring:@"hello"]);

But the closest I could find was:

if ([string rangeOfString:@"hello"] == 0) {
NSLog(@"sub string doesnt exist");
}
else {
NSLog(@"exists");
}

Anyway, is that the best way to find if a string contains another string?





RE: How do I check if a string contains another string in Objective-C? - suasiblemkb - 07-21-2023

Oneliner (Smaller amount of code. DRY, as you have only one `NSLog`):
<!-- language: lang-c -->

NSString *string = @"hello bla bla";
NSLog(@"String %@", ([string rangeOfString:@"bla"].location == NSNotFound) ? @"not found" : @"cotains bla");


RE: How do I check if a string contains another string in Objective-C? - cristiemjoywleqg - 07-21-2023

An improved version of [*P i*'s solution][1], a category on NSString, that not only will tell, if a string is found within another string, but also takes a range by reference, is:

@interface NSString (Contains)
-(BOOL)containsString: (NSString*)substring
atRange:(NSRange*)range;

-(BOOL)containsString:(NSString *)substring;
@end

@implementation NSString (Contains)

-(BOOL)containsString:(NSString *)substring
atRange:(NSRange *)range{

NSRange r = [self rangeOfString : substring];
BOOL found = ( r.location != NSNotFound );
if (range != NULL) *range = r;
return found;
}

-(BOOL)containsString:(NSString *)substring
{
return [self containsString:substring
atRange:NULL];
}

@end

Use it like:

NSString *string = @"Hello, World!";

//If you only want to ensure a string contains a certain substring
if ([string containsString:@"ello" atRange:NULL]) {
NSLog(@"YES");
}

// Or simply
if ([string containsString:@"ello"]) {
NSLog(@"YES");
}

//If you also want to know substring's range
NSRange range;
if ([string containsString:@"ello" atRange:&range]) {
NSLog(@"%@", NSStringFromRange(range));
}

[1]:

[To see links please register here]





RE: How do I check if a string contains another string in Objective-C? - lobotomising694495 - 07-21-2023

Here is a copy-and-paste function snippet:

-(BOOL)Contains:(NSString *)StrSearchTerm on:(NSString *)StrText
{
return [StrText rangeOfString:StrSearchTerm
options:NSCaseInsensitiveSearch].location != NSNotFound;
}



RE: How do I check if a string contains another string in Objective-C? - Mrprudenceew - 07-21-2023

If you need this once write:

NSString *stringToSearchThrough = @"-rangeOfString method finds and returns the range of the first occurrence of a given string within the receiver.";
BOOL contains = [stringToSearchThrough rangeOfString:@"occurence of a given string"].location != NSNotFound;




RE: How do I check if a string contains another string in Objective-C? - tendering95785 - 07-21-2023

So personally I really hate `NSNotFound` but understand its necessity.

But some people may not understand the complexities of comparing against NSNotFound

For example, this code:

- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
if([string rangeOfString:otherString].location != NSNotFound)
return YES;
else
return NO;
}

has its problems:

1) Obviously if `otherString = nil` this code will crash. a simple test would be:

NSLog(@"does string contain string - %@", [self doesString:@"hey" containString:nil] ? @"YES": @"NO");

results in !! CRASH !!

2) What is not so obvious to someone new to objective-c is that the same code will NOT crash when `string = nil`.
For example, this code:

NSLog(@"does string contain string - %@", [self doesString:nil containString:@"hey"] ? @"YES": @"NO");

and this code:

NSLog(@"does string contain string - %@", [self doesString:nil containString:nil] ? @"YES": @"NO");

will both result in

does string contains string - YES

Which is clearly NOT what you want.

So the better solution that I believe works is to use the fact that rangeOfString returns the length of 0 so then a better more reliable code is this:

- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
if(otherString && [string rangeOfString:otherString].length)
return YES;
else
return NO;
}

OR SIMPLY:

- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
return (otherString && [string rangeOfString:otherString].length);
}

which will for cases 1 and 2 will return

does string contains string - NO

That's my 2 cents ;-)

Please check out my [Gist][1] for more helpful code.


[1]:


RE: How do I check if a string contains another string in Objective-C? - newsmongery536519 - 07-21-2023

NSString *categoryString = @"Holiday Event";
if([categoryString rangeOfString:@"Holiday"].location == NSNotFound)
{
//categoryString does not contains Holiday
}
else
{
//categoryString contains Holiday
}


RE: How do I check if a string contains another string in Objective-C? - styxian281137 - 07-21-2023

If do not bother about case-sensitive string.
Try this once.

NSString *string = @"Hello World!";

if([string rangeOfString:@"hello" options:NSCaseInsensitiveSearch].location !=NSNotFound)
{
NSLog(@"found");
}
else
{
NSLog(@"not found");
}


RE: How do I check if a string contains another string in Objective-C? - southwestwardly133373 - 07-21-2023

NSString *myString = @"hello bla bla";
NSRange rangeValue = [myString rangeOfString:@"hello" options:NSCaseInsensitiveSearch];

if (rangeValue.length > 0)
{
NSLog(@"string contains hello");
}
else
{
NSLog(@"string does not contain hello!");
}
//You can alternatively use following too :

if (rangeValue.location == NSNotFound)
{
NSLog(@"string does not contain hello");
}
else
{
NSLog(@"string contains hello!");
}


RE: How do I check if a string contains another string in Objective-C? - vegetarian88122 - 07-21-2023

In case of swift, this can be used

let string = "Package #23"
if string.containsString("Package #") {
//String contains substring
}
else {
//String does not contain substring
}