【iOS】Objective-C 字符串操作

时间:2023-03-08 17:03:29

字符串中查找指定字符方法

//开头

-(BOOL) hasPrefix:(NSString *)aString;

//结尾

-(BOOL) hasSuffix:(NSString *)aString;

//中间

-(NSRange) rangeOfString:(NSString *)aString;

例如:

NSString *filename = @"draft-chapter.pages";
if([filename
hasPrefix:@"draft"])
{
}
if([filename
hasSuffix:@".mov"])
{
}

NSRange range;
range = [filename
rangeOfString:@"chapter"];
返回 range.location= 6 range.length
= 7
如果没有找到 range.location =
NSNotFound