1斜纪、substringFromIndex拯坟,substringToIndex咪橙,substringWithRange的用法层扶。
例如:
NSString?*str =?@"12345";
(1)substringFromIndex? 從第幾個字符開始。
//?NSString *subString0 = [str substringFromIndex:-1];???//會放生越界錯誤
NSString?*subString1 = [str?substringFromIndex:0]; ?? ? ?//@"12345"
NSString?*subString2 = [str?substringFromIndex:1]; ?? ? ?//@"2345"
NSString?*subString3 = [str?substringFromIndex:4]; ?? ? ?//@"5"
NSString?*subString4 = [str?substringFromIndex:5]; ?? ? ?//@""
//?NSString *subString5 = [str substringFromIndex:6]; ? //會放生越界錯誤
//?NSString *subString10 = [str substringToIndex:-1]; ? ?//會放生越界錯誤
(2)substringToIndex 到第幾個字符之前的字符都办。
NSString?*subString11 = [str?substringToIndex:0]; ?? ? ? ?//@"“
NSString?*subString12 = [str?substringToIndex:1]; ?? ? ? ?//@"1”
NSString?*subString13 = [str?substringToIndex:4]; ?? ? ? ?//@"1234“
NSString?*subString14 = [str?substringToIndex:5]; ?? ? ? ?//@"12345”
//?NSString *subString15 = [str substringToIndex:6]; ? ? //會放生越界錯誤
(3)substringWithRange 從第幾個字符之后的幾個字符嫡锌。
NSRange?range1 =?NSMakeRange(-1,?5);
NSRange?range2 =?NSMakeRange(0,?6);
NSRange?range3 =?NSMakeRange(0,?5);
NSRange?range4 =?NSMakeRange(1,?4);
NSRange?range5 =?NSMakeRange(0,?0);
NSRange?range6 =?NSMakeRange(4,?1);
//NSString *subString31 = [str substringWithRange:range1]; //會放生越界錯誤
//NSString *subString32 = [str substringWithRange:range2]; //會放生越界錯誤
NSString?*subString33 = [str?substringWithRange:range3]; ??//@"12345“
NSString?*subString34 = [str?substringWithRange:range4]; ??//@"2345”
NSString?*subString35 = [str?substringWithRange:range5]; ??//@"”
NSString?*subString36 = [str?substringWithRange:range6]; ??//@"5”
2、
NSString *str1 =@"this is string A";
NSString *str2 =@"this is string B";
(1)stringWithString 將一個字符串復(fù)制到另一個字符串
NSString *str = [NSString stringWithString : str1];
(2)stringByAppendingString琳钉,將一個字符串復(fù)制到另一個字符串的末尾:
str2 = [str1 stringByAppendingString:str2];
(3)uppercaseString,將字符串轉(zhuǎn)換為大寫势木。
? ? ? ? lowercaseString,將字符串轉(zhuǎn)換為小寫。
NSString *res =[str1 uppercaseString];
NSString *res =[str1 lowercaseString];
3歌懒、NSMutableString對象代表一個字符序列可變的字符串啦桌,而且NSMutableString是NSString的子類,因此前面介紹的NSString所包含的方法及皂,NSMutableString都可以直接使用甫男,NSMutableString對象也可直接當(dāng)成NSString對象使用。