这次总结下OC里一些对字符串的一些操作。
创建字符串对象时,会创建一个内容不可更改的对象,称为不可变对象。可以使用NSString类处理不可变字符串。你经常需要处理字符串并更改字符串中的字符。例如,可能希望从字符串中删除一些字符,或对字符串执行搜索替换操作。这种类型的字符串是使用NSMutableString类处理的。
//
// main.m
// Number_String_List_15
//
// Created by SeanAstin on 15/12/30.
// Copyright © 2015年 SeanAstin. All rights reserved.
// #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str1 = @"This is string A";
NSString *str2 = @"This is string B";
NSString *res;
NSComparisonResult compareResult; //计算字符串中的值
NSLog(@"Length of str1: %lu",[str1 length]); //将一个字符串复制到另一个字符串
res = [NSString stringWithString:str1];
NSLog(@"copy: %@",res); //将一个字符串复制到另一个字符串的末尾
str2 = [str1 stringByAppendingString:str1];
NSLog(@"Concatentation: %@",str2); //验证两个字符串是否相等
if([str1 isEqualToString:res] == YES)
NSLog(@"str1 == res");
else
NSLog(@"str1 != res"); //验证一个字符串是否小于、等于或大于另一个字符串
compareResult = [str1 compare:str2];
if(compareResult == NSOrderedAscending)
NSLog(@"str1 < str2");
else if(compareResult == NSOrderedSame)
NSLog(@"str1 == str2");
else//必须是NSorderedDescending
NSLog(@"str1 > str2"); //将字符串转换为大写
res = [str1 uppercaseString];
NSLog(@"Uppercase conversion: %s",[res UTF8String]); //将字符串转换为小写
res = [str1 lowercaseString];
NSLog(@"Lowercase conversion: %@",res);
}
return ;
}
//
// main.m
// Number_String_List_15
//
// Created by SeanAstin on 15/12/30.
// Copyright © 2015年 SeanAstin. All rights reserved.
// #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str1 = @"This is string A";
NSString *str2 = @"This is string B";
NSString *res;
NSRange subRange; //从字符串中提取前3个字符
res = [str1 substringToIndex:];
NSLog(@"First 3 chars of str1: %@",res); //提取从索引5开始直到结尾的子字符串
res = [str1 substringFromIndex:];
NSLog(@"Chars from index 5 of str1: %@",res); //提取从索引8开始索引到13的子字符串(6个字符)
res = [[str1 substringFromIndex:] substringToIndex:];
NSLog(@"Chars from index 8 through 13: %@",res); //更简单的方法
res = [str1 substringWithRange:NSMakeRange(, )];
NSLog(@"Chars from index 8 through 13: %@",res); //从另一个字符串中查找一个字符串
subRange = [str1 rangeOfString:@"string A"];
NSLog(@"String is at index %lu, length is %lu",subRange.location,subRange.length);
subRange = [str1 rangeOfString:@"string B"];
if(subRange.location == NSNotFound)
NSLog(@"String not found");
else
NSLog(@"String is at index %lu, length is %lu",subRange.location,subRange.length); }
return ;
}
NSMutableString类可以用来创建可以更改字符的字符串对象。因为是 NSString类的子类,所以可以使用NSString类的所有方法。
在讲述可变于不可变字符串对象时,我们谈到了更改字符串中的实际字符。任意一个可变或不可变字符串对象在程序执行期间,总是可以被设为完全不同的字符串对象的。
//
// main.m
// Number_String_List_15
//
// Created by SeanAstin on 15/12/30.
// Copyright © 2015年 SeanAstin. All rights reserved.
// #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str1 = @"This is string A";
NSString *search,*replace;
NSMutableString *mstr;
NSRange substr; //从不可变字符串创建可变字符串 mstr = [NSMutableString stringWithString:str1];
NSLog(@"%@",mstr); //插入字符
[mstr insertString: @" mutable" atIndex:];
NSLog(@"%@",mstr); //插入末尾进行有效拼接
[mstr insertString:@" and string B" atIndex:[mstr length]];
NSLog(@"%@",mstr); //直接使用appendString
[mstr appendString:@" and string C"];
NSLog(@"%@",mstr); //根据范围删除子字符串
[mstr deleteCharactersInRange:NSMakeRange(, )];
NSLog(@"%@",mstr); //查找然后将其删除
substr = [mstr rangeOfString:@"string B and "];
if(substr.location != NSNotFound){
[mstr deleteCharactersInRange:substr];
NSLog(@"%@",mstr);
} //直接设置为可变的字符串
[mstr setString:@"This is string A"];
NSLog(@"%@",mstr); //替换一些字符
[mstr replaceCharactersInRange:NSMakeRange(, ) withString:@"a mutable string"];
NSLog(@"%@",mstr); //查找和替换
search = @"This is";
replace = @"An example";
substr = [mstr rangeOfString: search];
if(substr.location != NSNotFound){
[mstr replaceCharactersInRange:substr withString:replace];
NSLog(@"%@",mstr);
} //查找和替换所有匹配项
search = @"a";
replace = @"X";
substr = [mstr rangeOfString:search];
while(substr.location != NSNotFound){
[mstr replaceCharactersInRange:substr withString:replace];
substr = [mstr rangeOfString:search]; }
NSLog(@"%@",mstr);
}
return ;
}