检查字符串中是否有内容

时间:2012-02-25 16:19:51

标签: xcode

我想检查字符串中是否有内容。 就像那样:

stringByReplacingOccurrencesOfString:@"h" withString:@"a"

但我想检查字符串中是否有“h”。

2 个答案:

答案 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");
}