Objective-C中判断字符串是否包含其他字符串


Objective-C中判断字符串是否包含其他字符串 

-(BOOL) hasPrefix:(NSString *) astring;检查字符串是否以astring开头;
-(BOOL) hasSuffix:(NSString *) astring;检查字符串是否以astring结尾;
使用方式:
NSString *filename=@"hello,world";
if([filename hasPrefix:@"hello"]){
...
}
if([filename hasSuffix:@"world"]){
...
}
如果想知道字符串内的某处是否包含其他的字符串,使用rangeOfString:
-(NSRange) rangeOfString:(NSString *) astring;
将rangeOfString:发送给一个NSString对象时,传递的参数时要查找的字符串。它会返回一个NSRange struct来告诉你一个与这个字符串相匹配的部分从哪里开始以及匹配上的字符个数。
NSRange range=[filename rangeOfString:@"o,world"];
if(range.location!=NSNotFound){
return true;
}else{
return false;
}

猜你喜欢

转载自sdlqhjk.iteye.com/blog/1743847