OC - NSString/NSMutablestring
2015-09-28 09:44
369 查看
//定义
不可变字符串.
NSString *s1 = @"abc";
//初始化
NSString *s2 = [[NSString alloc]init];
便利构造器
NSString *s3 = [NSString string];
//以上两个初始化方法都不常用.
//格式化初始化字符串
NSString *s4 = [[NSString alloc]initWithFormat:@"abc"];
NSLog(@"%@",s4);
NSString *s5 = [NSString stringWithFormat:@"def"];
NSLog(@"%@",s5);
NSString *s6 = [NSString stringWithFormat:@"%d123",456];
NSLog(@"%@",s6);
//通过文件路径获取文件里的内容.
NSString *s7 = [NSString stringWithContentsOfFile:@"/Users/lanou3g/Desktop/Xcode/OClesson - 4/OClesson - 4/123.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s7);
//表示求s6字符串长度.
NSLog(@"%lu",[s6 length]);
//判断是否有前后缀
NSString *s8 = @"abc123";
BOOL b1 = [s8 hasPrefix:@"abc"];
NSLog(@"%d",b1);
BOOL b2 = [s8 hasSuffix:@"123"];
NSLog(@"%d",b2);
//大小写转换
注意!!!一定要有返回值接收!!!
NSString *s9 = [s8 uppercaseString];
NSLog(@"%@",s9);
NSString *s10 =[s9 lowercaseString];
NSLog(@"%@",s10);
//将某个字符串里所有的单词都变成首字母大写;
NSMutableString
*d1 = [NSMutableString
stringWithFormat:@"i love you
somuch."];
NSLog(@"%@",d1);
NSString
*d2 = [d1
capitalizedString];
NSLog(@"%@",d
2);
//比较字符串
NSComparisonResult r1 = [s8 compare:s9];
NSLog(@"%ld",r1);
//比较字符串是否相等,也可以用第一个.
BOOL b3 = [s8 isEqualToString:s9];
NSLog(@"%d",b3);
//选取某位字符,(字符串计算从0位开始)
NSLog(@"%c",[s8 characterAtIndex:1]);
//从第x为开始截取字符串一直到最后,包括第x位.
NSString *s11 = [s8 substringFromIndex:1];
NSLog(@"%@",s11);
//从x位开始,包括第x位,以后的字符串全部截掉,只保留前x位.
NSString *s12 = [s8 substringToIndex:1];
NSLog(@"%@",s12);
//以字符串中的某个字符为界限,讲字符串分成两部分.
NSMutableString
*f1 = [NSMutableString
stringWithFormat:@"20|http://www.baidu.com"];
NSArray
*arr1 = [f1
componentsSeparatedByString:@"|"];
NSLog(@"%@",arr1);
打印结果(必须用数组接收):
20,
"http://www.baidu.com"
//从x位开始,截取长度为y的字符串.包括第x位.
NSString *s13 = [s8 substringWithRange:NSMakeRange(1, 3)];
NSLog(@"%@",s13);
////
//可变字符串.继承与nsstring,也就是说nsstring的所有方法nsmutablestring都可以使用.唯一的区别是可变字符串不建议用nsstring的最直接
//的方法赋初值.
NSMutableString *ms1 = [NSMutableString stringWithFormat:@"456"];
//设置或者修改值
[ms1 setString:@"789xyz"];
NSLog(@"%@",ms1);
//字符串拼接
[ms1 appendString:@"123"];
NSLog(@"%@",ms1);
[ms1 appendFormat:@"123%d",12];
NSLog(@"%@",ms1);
//在一个字符串中,截取其中某一段,替换为另一个字符串.
[ms1 replaceCharactersInRange:(NSMakeRange(3, 3)) withString:@"456"];
NSLog(@"%@",ms1);
//在一个字符串中,截取其中某一段,将其删除.
[ms1 deleteCharactersInRange:(NSMakeRange(3, 3))];
NSLog(@"%@",ms1);
//在第x位上插入某个字符或字符串.
[ms1 insertString:@"0" atIndex:3];
NSLog(@"%@",ms1);
不可变字符串.
NSString *s1 = @"abc";
//初始化
NSString *s2 = [[NSString alloc]init];
便利构造器
NSString *s3 = [NSString string];
//以上两个初始化方法都不常用.
//格式化初始化字符串
NSString *s4 = [[NSString alloc]initWithFormat:@"abc"];
NSLog(@"%@",s4);
NSString *s5 = [NSString stringWithFormat:@"def"];
NSLog(@"%@",s5);
NSString *s6 = [NSString stringWithFormat:@"%d123",456];
NSLog(@"%@",s6);
//通过文件路径获取文件里的内容.
NSString *s7 = [NSString stringWithContentsOfFile:@"/Users/lanou3g/Desktop/Xcode/OClesson - 4/OClesson - 4/123.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",s7);
//表示求s6字符串长度.
NSLog(@"%lu",[s6 length]);
//判断是否有前后缀
NSString *s8 = @"abc123";
BOOL b1 = [s8 hasPrefix:@"abc"];
NSLog(@"%d",b1);
BOOL b2 = [s8 hasSuffix:@"123"];
NSLog(@"%d",b2);
//大小写转换
注意!!!一定要有返回值接收!!!
NSString *s9 = [s8 uppercaseString];
NSLog(@"%@",s9);
NSString *s10 =[s9 lowercaseString];
NSLog(@"%@",s10);
//将某个字符串里所有的单词都变成首字母大写;
NSMutableString
*d1 = [NSMutableString
stringWithFormat:@"i love you
somuch."];
NSLog(@"%@",d1);
NSString
*d2 = [d1
capitalizedString];
NSLog(@"%@",d
2);
//比较字符串
NSComparisonResult r1 = [s8 compare:s9];
NSLog(@"%ld",r1);
//比较字符串是否相等,也可以用第一个.
BOOL b3 = [s8 isEqualToString:s9];
NSLog(@"%d",b3);
//选取某位字符,(字符串计算从0位开始)
NSLog(@"%c",[s8 characterAtIndex:1]);
//从第x为开始截取字符串一直到最后,包括第x位.
NSString *s11 = [s8 substringFromIndex:1];
NSLog(@"%@",s11);
//从x位开始,包括第x位,以后的字符串全部截掉,只保留前x位.
NSString *s12 = [s8 substringToIndex:1];
NSLog(@"%@",s12);
//以字符串中的某个字符为界限,讲字符串分成两部分.
NSMutableString
*f1 = [NSMutableString
stringWithFormat:@"20|http://www.baidu.com"];
NSArray
*arr1 = [f1
componentsSeparatedByString:@"|"];
NSLog(@"%@",arr1);
打印结果(必须用数组接收):
20,
"http://www.baidu.com"
//从x位开始,截取长度为y的字符串.包括第x位.
NSString *s13 = [s8 substringWithRange:NSMakeRange(1, 3)];
NSLog(@"%@",s13);
////
//可变字符串.继承与nsstring,也就是说nsstring的所有方法nsmutablestring都可以使用.唯一的区别是可变字符串不建议用nsstring的最直接
//的方法赋初值.
NSMutableString *ms1 = [NSMutableString stringWithFormat:@"456"];
//设置或者修改值
[ms1 setString:@"789xyz"];
NSLog(@"%@",ms1);
//字符串拼接
[ms1 appendString:@"123"];
NSLog(@"%@",ms1);
[ms1 appendFormat:@"123%d",12];
NSLog(@"%@",ms1);
//在一个字符串中,截取其中某一段,替换为另一个字符串.
[ms1 replaceCharactersInRange:(NSMakeRange(3, 3)) withString:@"456"];
NSLog(@"%@",ms1);
//在一个字符串中,截取其中某一段,将其删除.
[ms1 deleteCharactersInRange:(NSMakeRange(3, 3))];
NSLog(@"%@",ms1);
//在第x位上插入某个字符或字符串.
[ms1 insertString:@"0" atIndex:3];
NSLog(@"%@",ms1);
相关文章推荐
- OC - NSString
- NSArray/NSMutablearray
- Rich feature hierarchies for accurate object detection and semantic segmentation
- Objective-C中的Block
- OC-NSSTRING-NSMUTABLENSTRING
- Effective Objective-C(二)
- Effective Objective-C
- self在类方法中的使用
- 【IOS 开发学习总结-OC-21】★★★objective-c面向对象——委托/代理(delegate)
- Objective C中nil/Nil/NULL的区别
- 封装的概念和原理,set和get方法
- 类方法
- NSString字符串长度计算方法
- NSString类的介绍和用法
- object-c结构体赋值
- object-c Person类实现和分析
- Object转为String的几种形式
- object-c中对象创建
- object-c中类的声明和实现
- OC中的异常捕捉机制