原文地址:http://www.cnblogs.com/heyonggang/p/3449301.html
hasPrefix:方法的功能是判斷創(chuàng)建的字符串內(nèi)容是否以某個字符開始,其語法形式如下:
-(BOOL)hasPrefix:(NSString)aString;
其中,(NSString )aString;用來指定字符串。該方法的返回值為BOOL,當BOOL為YES或者為1時,則字符串是以某個字符開始;當BOOL為NO或者為0時,則字符串不是以某個字符開始。 【示例】以下程序通過使用hasPrefix:方法來判斷使用stringWithCString:方法創(chuàng)建的字符串是否以字母B開始,如果是以字母B開始,輸出開頭為字母B;如果不是,輸出開頭不為字母B。程序代碼如下:
#import <Foundation/Foundation.h>
int main(int argc,constchar* argv[])
{
@autoreleasepool{
NSString*a=[NSString stringWithCString:"ABCDEF" encoding:NSASCIIStringEncoding];
if([a hasPrefix:@"B"]==YES){//判斷字符串是否以B字符開始
NSLog(@"開頭為字母B");
}else{
NSLog(@"開頭不為字母B");
}
}
return0;
}
運行結(jié)果如下:
2013-03-1918:10:02.0874-13[751:303]開頭不為字母B