1. 程式人生 > >Objective-C 字串操作

Objective-C 字串操作

字串中查詢指定字元方法

//開頭

-(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.start = 6 range.length = 7
如果沒有找到 range.start = NSNotFound