我想检查字符串中是否有内容。 就像那样:
stringByReplacingOccurrencesOfString:@"h" withString:@"a"
但我想检查字符串中是否有“h”。
答案 0 :(得分:1)
你去:
NSString *theString = @"just any string that you want";
NSRange match;
match = [theString rangeOfString: @"you"];
if (match.location == NSNotFound)
NSLog (@"Sorry, not found.");
else
NSLog (@"Match found at index %i", match.location);
享受。
答案 1 :(得分:0)
考虑[NSString rangeOfString: @"h"]
,如果字符串中存在“h”字符,则该方法返回的NSRange将有效。
即
NSRange rangeOfTheCharacterIWant = [myString rangeOfString: @"h"];
if(rangeOfTheCharacterIWant.location != NSNotFound)
{
NSLog( @"h exists!");
} else {
NSLog( @"h does not exist");
}