Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone : How to check whether a substring exists in a string?

I have some string name and I want to compare whether that string contains substring like "_thumb.png".

like image 680
Devang Avatar asked Apr 06 '11 10:04

Devang


People also ask

How do I check if a string contains a substring?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do you check if a value is present in a string?

find() str. find() method is generally used to get the lowest index at which the string occurs, but also returns -1, if string is not present, hence if any value returns >= 0, string is present, else not present.

How do you check if a substring is in a string Swift?

To check if a string contains another string, we can use the built-in contains() method in Swift. The contains() method takes the string as an argument and returns true if a substring is found in the string; otherwise it returns false .

How do you check if a character exists in a string?

Use the String. indexOf() method to check if a string contains a character, e.g. if (str. indexOf(char) !== -1) {} .


2 Answers

[string rangeOfString:string1].location!=NSNotFound 

rangeOfString: documentation.

like image 188
unexpectedvalue Avatar answered Sep 22 '22 08:09

unexpectedvalue


You can try this:

NSString *originalString; NSString *compareString; 

Let your string be stored in originalString and the substring that you want to compare is stored in compareString.

if ([originalString rangeOfString:compareString].location==NSNotFound) {            NSLog(@"Substring Not Found");   } else {     NSLog(@"Substring Found Successfully"); } 
like image 45
Gypsa Avatar answered Sep 20 '22 08:09

Gypsa