NSString * string=@"2011-11-29 12-11-10";
1查找
NSRange range = [string rangeOfString:@"-"];
if (range.location == NSNotFound)
{
? ? ? NSLog(@"str中沒(méi)有找到-");
}else {
? ? ?NSLog(@"搜索范圍%@",NSStringFromRange(range));
}
NSLiteralSearch區(qū)分大小寫(完全比較)
NSCaseInsensitiveSearch不區(qū)分大小寫
NSNumericSearch只比較字符串的個(gè)數(shù)钻蔑,而不比較字符串的字面值
從尾開始搜索:
range = [str rangeOfString:@"456" options:NSBackwardsSearch] ;
NSLog(@"從尾搜索的范圍%@",NSStringFromRange(range));
ios8.0以后使用,也是判斷是否包含某個(gè)字符串:
BOOL result = [@“123" containsString:@"2"];
2替換
? string=[string stringByReplacingOccurrencesOfString:@"-"withString:@"/"]; ? ? ? ?
NSLog(@"replaceStr=%@",string);
3截取
NSString*bStr = [string substringWithRange:NSMakeRange(loc,2)];
loc表示從第幾個(gè)字符開始奸鸯,2表示截取兩個(gè)字符咪笑;
[str substringToIndex:n];
從首字符開始截取n個(gè)字符;
[string substringFromIndex:n];
從第n+1位開始截取,至字符串最后;
4拆分
NSString*string =@"11111111A22222222";
NSArray*array = [string componentsSeparatedByString:@"A"];//從字符A中分隔成2個(gè)元素的數(shù)組
NSLog(@"array:%@",array);
NSLog(@"array1 ==== %@",[array firstObject]);
NSLog(@"array2 ==== %@",[array lastObject]);
5.驗(yàn)證
NSString*str1 =@"http:www.baidu.com";
BOOL a = [str1 hasPrefix:@"http"];//是否是以http開頭
BOOL b = [str1 hasSuffix:@"com"];//文件路徑是否以com結(jié)尾
NSLog(@"a ===== %d ,b ==== %d",a,b);
6.改變字符串的大小寫
NSString* str2 =@"beiJING";
NSString*da = [str2 uppercaseString];//轉(zhuǎn)成大寫
NSString*xiao = [str2 lowercaseString];//轉(zhuǎn)成小寫
NSString*shouDa = [str2 capitalizedString];//首字母大寫,其余小寫
NSLog(@"\n大寫==%@\n小寫=== %@\n首字母大寫其余小寫=== %@",da,xiao,shouDa);
7.編碼府喳,解碼
編碼
NSString*hStr =@"你好啊";
NSString*hString = [hStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"hString === %@",hString);
解碼
NSString*str3 =@"\u5982\u4f55\u8054\u7cfb\u5ba2\u670d\u4eba\u5458\uff1f";
NSString*str5 = [str3stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"string ==== %@",str5);