1.截取字符串
NSString *string =@"123456d890";
NSString *str1 = [string substringToIndex:5];//截取掉下標(biāo)5之前的字符串
NSLog(@"截取的值為:%@",str1);
NSString *str2 = [string substringFromIndex:3];//截取掉下標(biāo)3之后的字符串
NSLog(@"截取的值為:%@",str2);
2.匹配字符串
從字符串(sd是sfsfsAdfsdf)中查找(匹配)字符串(Ad)
NSString *string =@"sd是sfsfsAdfsdf";
NSRange range = [string rangeOfString:@"Ad"];//匹配得到的下標(biāo)
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范圍內(nèi)的字符串
NSLog(@"截取的值為:%@",string);
3.分隔字符串
根據(jù)字符串中的某個(gè)字符(A)來分割字符串
NSString *string =@"sdfsfsfsAdfsdf";
NSArray *array = [string componentsSeparatedByString:@"A"]; //從字符A中分隔成2個(gè)元素的數(shù)組
NSLog(@"array:%@",array); //結(jié)果是adfsfsfs和dfsdf
*判斷contents是否含有aaa
if([contents rangeOfString:@"aaa"].location !=NSNotFound)//_roaldSearchText
{
NSLog(@"yes");
}else{
NSLog(@"no");
}