2011-07-29 38 views
3

的一部分,我有這樣的NSString:IOS:檢查的NSString

NSString *test = @"three is a number"; 

我要檢查,如果這NSString的開頭 「三化」

回答

15

嘗試使用hasPrefix方法:

if([test hasPrefix:@"three"]) 
{ 
    // Begins with three 
}
3

它是由

if([test hasPrefix:@"three"]){ 
    ... 
} 

籠統的回答做:當你要檢查NSString(或任何Apple提供的類)是否查看官方文檔!在這種情況下,請參閱here。你看,互聯網上不僅有StackOverflow,還有官方文檔!

4

對於更一般的方法,獲取相關的子字符串,並將其與期望的結果進行比較。

if ([[test substringWithRange:NSMakeRange(0,5)] caseInsensitiveCompare:@"three"] == NSOrderedSame) { 
    // .... 
} 
2

您需要使用以下方法:

if ([test hasPrefix:@"three"]) 
{ 
    // if YES 
}