1、尋找
NSString*str = @"bfjfnnfkanfknf";
NSRange range = [str rangeOfString:@"111"];
if (range.location == NSNotFound)
{
NSLog(@"str中沒有找到111");
}else {
NSLog(@"搜尋範圍%@",NSStringFromRange(range));
}
下面的方法在ios8.0以後使用,也是判斷是否包含某個字串
BOOL result = [@“123" containsString:@"2"]; 2、截取
NSString*bStr = [str substringWithRange:NSMakeRange(loc,2)];
loc表示從第幾個字元開始,2表示截取兩個字元;
從首字元開始截取n個字元: [str substringToIndex:n]; 從第n+1位開始截取,至字串最後: [str substringFromIndex:n];
3、拆分
NSString*string =@"面朝大海/春暖花開";
NSArray*array = [string componentsSeparatedByString:@"/"];//從字元/中分隔成2個元素的數組
NSLog(@"array1 ==== %@",[array firstObject]);
NSLog(@"array2 ==== %@",[array lastObject]); 列印結果:array1 = 面朝大海,array2 = 春暖花開 4.替換
NSString*str =@"12345678";
NSString*replacedStr = [str stringByReplacingOccurrencesOfString:@"345"withString:@"想要替換成的字串"];
NSLog(@"replacedStr ==== %@", replacedStr);
列印結果:
replacedStr ==== 12想要替換成的字串678 5.驗證
NSString*str1 =@"http:www.baidu.com";
BOOL a = [str1 hasPrefix:@"http"];//是否是以http開頭
BOOL b = [str1 hasSuffix:@"com"];//檔案路徑是否以com結尾
NSLog(@"a ===== %d ,b ==== %d",a,b);
列印結果:
a ===== 1 ,b ==== 1 6.轉成數字
[str boolValue];
[str integerValue]; 7.改變字串的大小寫
NSString* str2 =@"beiJING";
NSString*da = [str2 uppercaseString];//轉成大寫
NSString*xiao = [str2 lowercaseString];//轉成小寫
NSString*shouDa = [str2 capitalizedString];//首字母大寫,其餘小寫
NSLog(@"\n大寫==%@\n小寫=== %@\n首字母大寫其餘小寫=== %@",da,xiao,shouDa);
列印結果:
大寫==BEIJING
小寫=== beijing
首字母大寫其餘小寫=== Beijing 8.編碼,解碼
編碼:
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);
列印結果:
hString === %E4%BD%A0%E5%A5%BD%E5%95%8A
string ====如何聯絡客服人員。