I have some string name and I want to compare whether that string contains substring like "_thumb.png".
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.
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.
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 .
Use the String. indexOf() method to check if a string contains a character, e.g. if (str. indexOf(char) !== -1) {} .
[string rangeOfString:string1].location!=NSNotFound
rangeOfString: documentation.
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"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With