这次总结下OC里一些对字符串的一些操作。
创建字符串对象时,会创建一个内容不可更改的对象,称为不可变对象。可以使用NSString类处理不可变字符串。你经常需要处理字符串并更改字符串中的字符。例如,可能希望从字符串中删除一些字符,或对字符串执行搜索替换操作。这种类型的字符串是使用NSMutableString类处理的。
1 // 2 // main.m 3 // Number_String_List_15 4 // 5 // Created by SeanAstin on 15/12/30. 6 // Copyright © 2015年 SeanAstin. All rights reserved. 7 // 8 9 #import10 11 int main(int argc, const char * argv[]) {12 @autoreleasepool {13 NSString *str1 = @"This is string A";14 NSString *str2 = @"This is string B";15 NSString *res;16 NSComparisonResult compareResult;17 18 //计算字符串中的值19 NSLog(@"Length of str1: %lu",[str1 length]);20 21 //将一个字符串复制到另一个字符串22 res = [NSString stringWithString:str1];23 NSLog(@"copy: %@",res);24 25 //将一个字符串复制到另一个字符串的末尾26 str2 = [str1 stringByAppendingString:str1];27 NSLog(@"Concatentation: %@",str2);28 29 //验证两个字符串是否相等30 if([str1 isEqualToString:res] == YES)31 NSLog(@"str1 == res");32 else33 NSLog(@"str1 != res");34 35 //验证一个字符串是否小于、等于或大于另一个字符串36 compareResult = [str1 compare:str2];37 if(compareResult == NSOrderedAscending)38 NSLog(@"str1 < str2");39 else if(compareResult == NSOrderedSame)40 NSLog(@"str1 == str2");41 else//必须是NSorderedDescending42 NSLog(@"str1 > str2");43 44 //将字符串转换为大写45 res = [str1 uppercaseString];46 NSLog(@"Uppercase conversion: %s",[res UTF8String]);47 48 //将字符串转换为小写49 res = [str1 lowercaseString];50 NSLog(@"Lowercase conversion: %@",res);51 }52 return 0;53 }
1 // 2 // main.m 3 // Number_String_List_15 4 // 5 // Created by SeanAstin on 15/12/30. 6 // Copyright © 2015年 SeanAstin. All rights reserved. 7 // 8 9 #import10 11 int main(int argc, const char * argv[]) {12 @autoreleasepool {13 NSString *str1 = @"This is string A";14 NSString *str2 = @"This is string B";15 NSString *res;16 NSRange subRange;17 18 //从字符串中提取前3个字符19 res = [str1 substringToIndex:3];20 NSLog(@"First 3 chars of str1: %@",res);21 22 //提取从索引5开始直到结尾的子字符串23 res = [str1 substringFromIndex:5];24 NSLog(@"Chars from index 5 of str1: %@",res);25 26 //提取从索引8开始索引到13的子字符串(6个字符)27 res = [[str1 substringFromIndex:8] substringToIndex:6];28 NSLog(@"Chars from index 8 through 13: %@",res);29 30 //更简单的方法31 res = [str1 substringWithRange:NSMakeRange(8, 6)];32 NSLog(@"Chars from index 8 through 13: %@",res);33 34 //从另一个字符串中查找一个字符串35 subRange = [str1 rangeOfString:@"string A"];36 NSLog(@"String is at index %lu, length is %lu",subRange.location,subRange.length);37 subRange = [str1 rangeOfString:@"string B"];38 if(subRange.location == NSNotFound)39 NSLog(@"String not found");40 else41 NSLog(@"String is at index %lu, length is %lu",subRange.location,subRange.length);42 43 }44 return 0;45 }
NSMutableString类可以用来创建可以更改字符的字符串对象。因为是 NSString类的子类,所以可以使用NSString类的所有方法。
在讲述可变于不可变字符串对象时,我们谈到了更改字符串中的实际字符。任意一个可变或不可变字符串对象在程序执行期间,总是可以被设为完全不同的字符串对象的。
1 // 2 // main.m 3 // Number_String_List_15 4 // 5 // Created by SeanAstin on 15/12/30. 6 // Copyright © 2015年 SeanAstin. All rights reserved. 7 // 8 9 #import10 11 int main(int argc, const char * argv[]) {12 @autoreleasepool {13 NSString *str1 = @"This is string A";14 NSString *search,*replace;15 NSMutableString *mstr;16 NSRange substr;17 18 //从不可变字符串创建可变字符串19 20 mstr = [NSMutableString stringWithString:str1];21 NSLog(@"%@",mstr);22 23 //插入字符24 [mstr insertString: @" mutable" atIndex:7];25 NSLog(@"%@",mstr);26 27 //插入末尾进行有效拼接28 [mstr insertString:@" and string B" atIndex:[mstr length]];29 NSLog(@"%@",mstr);30 31 //直接使用appendString32 [mstr appendString:@" and string C"];33 NSLog(@"%@",mstr);34 35 //根据范围删除子字符串36 [mstr deleteCharactersInRange:NSMakeRange(16, 13)];37 NSLog(@"%@",mstr);38 39 //查找然后将其删除40 substr = [mstr rangeOfString:@"string B and "];41 if(substr.location != NSNotFound){42 [mstr deleteCharactersInRange:substr];43 NSLog(@"%@",mstr);44 }45 46 //直接设置为可变的字符串47 [mstr setString:@"This is string A"];48 NSLog(@"%@",mstr);49 50 //替换一些字符51 [mstr replaceCharactersInRange:NSMakeRange(8, 8) withString:@"a mutable string"];52 NSLog(@"%@",mstr);53 54 //查找和替换55 search = @"This is";56 replace = @"An example";57 substr = [mstr rangeOfString: search];58 if(substr.location != NSNotFound){59 [mstr replaceCharactersInRange:substr withString:replace];60 NSLog(@"%@",mstr);61 }62 63 //查找和替换所有匹配项64 search = @"a";65 replace = @"X";66 substr = [mstr rangeOfString:search];67 while(substr.location != NSNotFound){68 [mstr replaceCharactersInRange:substr withString:replace];69 substr = [mstr rangeOfString:search];70 71 }72 NSLog(@"%@",mstr);73 }74 return 0;75 }