Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值。而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度。
NSString 常用方法总结
+(id)stringWithContentsOfFile:path encoding:enc err |
创建一个新字符串并将其设置为 path 指定文件的内容,使用字符编码 enc,在err 上返回错误 |
+(id)stringWithContentsOfURL:url encoding:enc err |
创建一个新字符串并将其设置为 url 所指向的内容,使用字符编码 enc,在err 上返回错误 |
+(id)string |
创建一个新的空字符串 |
+(id)stringWithString:nsstring |
创建一个新字符串,将其内容设置为 nsstring 内容 |
-(id)initWithString:nsstring |
将字符串内容设置为 nsstring 内容 |
-(id)initWithContentsOfFile:path encoding:enc error:err |
将字符串设置为 path 指定文件的内容,使用字符编码 enc,在err 上返回错误 |
-(id)initWithContentsOfURL:url encoding:enc error:err |
将字符串设置为 url 所指向的内容,使用字符编码 enc,在err 上返回错误 |
-(UNSigned int)length |
返回字符串中字符数目 |
-(unichar)characterAtindex:i |
返回索引i 所在UniCode 字符 |
-(NSString*)substringFromIndex:i |
返回从索引 i 开始到结尾的子字符串 |
-(NSString*)substringWithRange:range |
根据指定范围返回子字符串 |
-(NSString*)substringToIndex:i |
返回从字符串开始到 索引 i 的子字符串 |
-(NSComparator*)caseInsensitiveCompare:nsstring |
比较两个字符串大小,忽略大小写 |
-(NSComparator*)compare:nsstring |
比较两个字符串大小 |
-(BOOL)hasPrefix:nsstring |
测试字符串是否以 nsstring 开始 |
-(BOOL)hasSuffix:nsstring |
测试字符串是否以 nsstring 结尾 |
-(BOOL)isEqualToString:nsstring |
测试两个字符串是否相等 |
-(NSString*)capitalizedString |
返回字符串,串中的每个单词的首字母大写,其余字母小写 |
-(NSString*)lowercaseString |
返回转换为小写的字符串 |
-(NSString*)uppercaseString |
返回转换为大写的字符串 |
-(const char*)UTF8String |
返回UTF8编码格式的字符串 |
-(double)doubleValue |
返回转换为double 类型的字符串 |
-(float)floatValue |
返回转换为 float 类型的字符串 |
-(NSInteger)integerValue |
返回转换为 NSInteger 类型的新字符串 |
-(int)intvalue |
返回转换为 int 的字符串 |
NSMutableString 可修改字符串常用方法
+(id)stringWithCapacity:size |
创建一个字符串,容量为size大小 |
-(id)initWithCapacity:size |
初始化一个字符串,容量为size |
-(void)setString:nsstring |
将字符串设置为 nsstring |
-(void)appendString:nsstring |
在字符串末尾追加字符串 nsstring |
-(void)deleteCharatersInRange:range |
删除指定range 中的字符 |
-(void)insertString:nsstring atIndex:i |
以索引 i 为起始位置插入 nsstring |
-(void)replaceCharatersInRange;range withString:nsstring |
使用 nsstring 替换 range 指定的字符 |
-(void)replaceOccurrencesOfString:nsstring withString:nsstring2 options:opts range:range |
根据选项 opts ,使用指定 range 中的nsstring2 替换所有的 nsstring |
来源:
http://blog.csdn.net/xys289187120/article/details/6777283
http://www.cocoachina.com/iphonedev/sdk/2010/0607/1634.html
http://blog.csdn.net/ioswyl88219/article/details/30457591
1.创建NSString字符串
NSString 与 char* 最大的区别就是 NSString是一个objective对象,而char* 是一个字节数组。@+" 字符串 " 这个符号为objective-c NSString 字符串常量的标准用法,char* 创建的时候 无需添加@
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSString *str0 = @"my name is justcoding !";
-
-
-
- NSString *str1 = [NSString stringWithFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"justcoding@gmail.com"];
-
-
- NSString *str2 = [NSString stringWithString:@"我是字符串"];
-
-
- NSString *str3 = [NSString stringWithUTF8String:"字符串转换utf-8格式"];
-
-
-
- int i = 100;
- char*c = "xuanyusong";
- NSString *temp = @"我是临时字符串";
-
-
- NSString *str4 = [temp stringByAppendingFormat:@"整型: %d 字符型 :%s",i,c];
-
-
- NSString *str5 = [temp stringByAppendingString:temp];
-
-
- NSLog(@"str0 = %@", str0);
- NSLog(@"str1 = %@", str1);
- NSLog(@"str2 = %@", str2);
- NSLog(@"str3 = %@", str3);
- NSLog(@"str4 = %@", str4);
- NSLog(@"str5 = %@", str5);
-
- }
2.字符串的遍历
每一个字符串其实是由若干个char字符组成,字符串的遍历实际上就是将字符串中的每一个字符提取出来。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSString *str = @"YUSONGMOMO";
-
-
- int count = [str length];
-
- NSLog(@"字符串的长度是%d",count);
-
-
- for(int i =0; i < count; i++)
- {
- char c = [str characterAtIndex:i];
- NSLog(@"字符串第 %d 位为 %c",i,c);
- }
-
- }
3.字符串的比较
isEqualToString 比较字符串是否完全相等,大小写不一样也无法完全匹配。
hasPrefixe 匹配字符串头
haSuffix 匹配字符串的尾巴
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSString *str0 = @"justcoding";
- NSString *str1 = @"justcoding";
-
-
-
- if([str0 isEqualToString:str1])
- {
- NSLog(@"字符串完全相等");
- }
-
-
- if([str0 hasPrefix:@"just"])
- {
- NSLog(@"字符串str0以just开头");
- }
-
-
- if([str1 hasSuffix:@"coding"])
- {
- NSLog(@"str1字符串以coding结尾");
- }
- }
- NSString *astring01 = @"This is a String!";
- NSString *astring02 = @"This is a String!";
- BOOL result = [astring01 isEqualToString:astring02];
- NSLog(@"result:%d",result);
- NSString *astring01 = @"This is a String!";
- NSString *astring02 = @"This is a String!";
- BOOL result = [astring01 compare:astring02] = = NSOrderedSame;
- NSLog(@"result:%d",result);
- NSString *astring01 = @"This is a String!";
- NSString *astring02 = @"this is a String!";
- BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;
- NSLog(@"result:%d",result);
- NSString *astring01 = @"this is a String!";
- NSString *astring02 = @"This is a String!";
- BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;
- NSLog(@"result:%d",result);
- NSString *astring01 = @"this is a String!";
- NSString *astring02 = @"This is a String!";
- BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
- NSLog(@"result:%d",result);
- NSString *astring01 = @"this is a String!";
- NSString *astring02 = @"This is a String!";
- BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;
- NSLog(@"result:%d",result);
4. 文件字符串操作(读写)
- NSString *path = @"astring.text";
- NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
- NSLog(@"astring:%@",astring);
- [astring release];
- NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
- NSLog(@"astring:%@",astring);
- NSString *path = @"astring.text";
- [astring writeToFile: path atomically: YES];
- [astring release];
* ios5 不支持release iOS应用开发:什么是ARC?
- NSString *Path = @"~/NSData.txt";
- NSString *absolutePath = [Path stringByExpandingTildeInPath];
- NSLog(@"absolutePath:%@",absolutePath);
- NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
- NSString *Path = @"~/NSData.txt";
- NSLog(@"Extension:%@",[Path pathExtension]);
5. 字符串的截取和大小写
- NSString *string1 = @"A String";
- NSString *string2 = @"String";
- NSLog(@"string1:%@",[string1 uppercaseString]);
- NSLog(@"string2:%@",[string2 lowercaseString]);
- NSLog(@"string2:%@",[string2 capitalizedString]);
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSString *str0 = @"中文my name is xuanyusong";
-
-
- NSString * to = [str0 substringToIndex:4];
-
- NSLog(@"to = %@",to);
-
-
- NSString * from = [str0 substringFromIndex:2];
-
- NSLog(@"from = %@",from);
-
-
-
-
- NSRange rang = NSMakeRange(2, 10);
- NSString * strRang = [str0 substringWithRange:rang];
- NSLog(@"rang = %@",strRang);
-
-
- NSLog(@"str0首字母大写:%@",[str0 capitalizedString]);
-
- NSLog(@"str0大写:%@",[str0 uppercaseString]);
-
- NSLog(@"str0小写:%@",[str0 lowercaseString]);
-
- }
6.搜索字符串与替换字符串
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSString *str0 = @"中文my name is xuanyusong";
- NSString *temp = @"is";
- NSRange rang = [str0 rangeOfString:temp];
-
- NSLog(@"搜索的字符串在str0中起始点的index 为 %d", rang.location);
- NSLog(@"搜索的字符串在str0中结束点的index 为 %d", rang.location + rang.length);
-
-
- NSString *str = [str0 stringByReplacingCharactersInRange:rang withString:@"哇咔咔卡卡咔"];
- NSLog(@"替换后字符串为%@", str);
-
-
-
- str = [str0 stringByReplacingOccurrencesOfString :@" " withString:@"@"];
- NSLog(@"替换后字符串为%@", str);
-
-
- }
拓展:使用下面这个方法整体替换字符串还可以设置替换的区域。
stringByReplacingOccurrencesOfString:(NSString *)withString:(NSString *) options:(NSStringCompareOptions) range:(NSRange)
7.字符串尾部添加
使用alloc在内存中创建字符串对象后边可以动态的操作这个字符串,修改与添加等。
appendstring 方法:向字符串尾部添加一个字符串。
appendFormat方法:向字符串尾部添加多个类型的字符串,可以添加任意数量与类型的字符串。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSMutableString * str = [[NSMutableString alloc] init];
-
-
- [str appendString:@"aaa"];
-
-
- [str appendFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"justcoding@gmail.com"];
-
- NSLog(@"str = %@",str);
-
- }
8. 字符串中删除元素
stringWithString 方法:用于创建字符串初始化赋值
rangeOfString方法:传入字符串返回一个在该字符串中的范围 也可以写 NSMakeRange(0, 3) 意思是范围在字符串0位到第3位
deleteCharactersInRange:删除字符串 参数为Range 就是删除的范围。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSMutableString *str = [NSMutableString stringWithString:@"justcoding is best!"];
-
-
- [str deleteCharactersInRange: [str rangeOfString: @"justcoding"]];
-
- NSLog(@"str = %@",str);
-
- }
9. 字符串插入
stringWithString 方法:用于创建字符串初始化赋值
insertString方法:第一个参数 插入的字符串对象, 第二个参数 插入的位置。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSMutableString *str = [NSMutableString stringWithString:@"justcoding is"];
-
-
- [str insertString:@"best man" atIndex:10];
-
- NSLog(@"str = %@",str);
-
- }
10. 字符串拷贝
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSMutableString *str1 = [NSMutableString stringWithString: @"字符串1"];
- NSMutableString *str2;
-
-
- str2 = str1;
-
- [str2 appendString: @" 和字符串2"];
-
- NSLog (@"str1 = %@", str1);
-
- NSLog (@"str2 = %@", str2);
- }
为什么给str2添加数据后 str1的数据也改变了?这就是指针的魅力所在,因为我们操作的是指针,str2 = str1 意思是两个指针同时指向一块内存,那么str2指向的内存内容改变以后str1当然跟着改变了。
11. 字符串与指定类型转换
如果转换的参数非法的话不会抛出异常,好比用中文去转整型。不会报错 但是转换结果为 0 ,默认值。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSString *str0 = @"1121";
-
-
-
- int i = [str0 intValue];
-
- NSLog (@"转换后:%i", i);
-
-
- NSString *str1 = @"1985";
-
-
-
- NSInteger ii = [str1 integerValue];
-
- NSLog (@"转换后:%i", ii);
-
-
-
-
- NSString *str2 = @"3.145926";
-
-
-
- double d = [str2 doubleValue];
-
- NSLog (@"转换后:%f", d);
-
-
- NSString *str3 = @"3.145926";
-
-
- double f = [str3 floatValue];
-
- NSLog (@"转换后:%f", f);
-
- }