C String Function: Contains String
How can I check if an NSString contains another substring, at which point it will return a Boolean Value.
This is what I'm thinking of:
If myString.contains("string") then
{
//Stuff Happens
}
But, from the research I've done, it seems as if Obj-C has no function for this. This Wikipedia article gives numerous string functions, their differences, and all in different languages, but I see no Obj-C support for any Contain Function.
Does anyone know of a simple-to-use function like the once above (which is similar to the C# and VB.NET function)?
Would a "Find" Function work? If so, how?
If this is not supported in Obj-C, is there a workaround I can use?
Any help is very appreciated.
if ([myString rangeOfString:@"string"].location != NSNotFound)
{
// Stuff happens
}
NSString *someString = @"Time for an egg hunt";
if ( [someString rangeOfString:@"egg" options:NSCaseInsensitiveSearch].location != NSNotFound ) {
NSLog( @"Found it!" );
}
If you want to be case insensitive
.
NSRange range = [string rangeOfString:@"string" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound)
{
return range.location;
}
else
{
return nil;
}
Documentation
链接地址: http://www.djcxy.com/p/73998.html下一篇: C字符串功能:包含字符串