1.创建NSString对象
点击(此处)折叠或打开
1. void stringCreate() {
2. //这种方式创建,不需要释放内存
3. NSString *str1 = @"A String";//
4. //
5. NSString *str2 = [[NSString alloc] init];
6. str2 = @"B String";
7. [str2 release];
8.
9. NSString *str3 = [[NSString alloc]initWithString:@"C string!"];
10. [str3 release];
11.
12. //静态方法创建对象,不需要管理内存
13. str3 = [NSString stringWithString:@"c string!"];
14.
15. NSString *str4 = [[NSString alloc] initWithUTF8String:"D string!"];
16. [str4 release];
17.
18. NSString *str5 = [[NSString alloc] initWithFormat:
19. @"my age is %i and height is %.2f",28,1.65f];
20. NSLog(@"str5:%@",str5);
21. [str5 release];
22. }
2.NSString的导入与导出:从文件或url读取内容,以及写文件
点击(此处)折叠或打开
3.NSString常用方法:
1. void stringCreate2(){
2. NSString *path=@"/Users/luteresa/Desktop/hello.txt";
3. //NSString *str1 = [NSString stringWithContentsOfFile:path];
4. NSError *perror;
5. NSString *str2 = [NSString stringWithContentsOfFile:path encoding:(NSUTF8StringEncoding) error:&perror];
6. if (perror) {
7. NSLog(@"读取错误:%@",perror);
8. } else {
9. NSLog(@"%@",str2);
10. }
11. }
12. void stringCreate3(){
13. NSString *path = @"/Users/luteresa/Desktop/abc/abc.txt";
14.
15. //NSURL *url = [NSURL URLWithString:@"file:///Users/luteresa/Desktop/abc.txt"];
16. // NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
17. // NSString *str2 = [NSString stringWithContentsOfURL: url encoding:NSUTF8StringEncoding error:nil];
18. // //NSLog(@"%@",str2);
19. NSString *str = @"123456";
20. NSError *perror;
21. //[perror localizedDescription];
22. [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&perror];
23. if (perror) {
24. NSLog(@"写入失败,%@", [perror localizedDescription]);
25. } else {
26. NSLog(@"写入成功");
27. }
28. }
29. void stringToFile(){
30. NSString *path = @"/Users/luteresa/Desktop/test.txt";
31. NSString *str1 = @"123457890";
32. [str1 writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
33. }
(1) 大小写处理
点击(此处)折叠或打开
(NSString *)uppercaseString; //全部转为大写字符
- (NSString *)lowercaseString; //全部转为小写字母
- (NSString *)capitalizedString;//首字符变大写,其他变小写
(2)
字符串比较
点击(此处)折叠或打开
1. void compare() {
2. // 检测字符串的内容是否相同
3. BOOL result = [@"abc" isEqualToString:@"abc"];
4. NSLog(@"%i", result);
5.
6. // NSOrderedAscending 右边的字符串比左边大
7. // NSOrderedSame 两个字符串的内容相同
8. // NSOrderedDescending 左边的字符串比右边的大
9. NSComparisonResult result2 = [@"abc" compare:@"Abc"];
10. if (result2 == NSOrderedSame) {
11. NSLog(@"两个字符串的内容相同");
12. } else if (result2 == NSOrderedAscending) {
13. NSLog(@"右边 > 左边");
14. } else if (result2 == NSOrderedDescending) {
15. NSLog(@"右边 < 左边");
16. }
17. }
还有一个忽略大小写的比较方法,用法余compare一样
点击(此处)折叠或打开
- - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
(3)
搜索字符串
点击(此处)折叠或打开
1. void search() {
2. NSString *str = @"123456456.txt";
3.
4. NSLog(@"是否以22开头:%i", [str hasPrefix:@"22"]);
5. NSLog(@"是否以txt结尾:%i", [str hasSuffix:@"txt"]);
6.
7. // 搜索字符串
8. NSRange range = [str rangeOfString:@"456"];
9. // range.length == 0
10. if (range.location == NSNotFound) {
11. NSLog(@"不能找到");
12. } else {
13. NSLog(@"找到的范围是:%@", NSStringFromRange(range));
14. }
15.
16. // 从尾部开始搜索字符串
17. range = [str rangeOfString:@"456" options:NSBackwardsSearch];
18. NSLog(@"%@", NSStringFromRange(range));
19.
20. // 指定范围进行搜索
21. range = [str rangeOfString:@"456" options:NSBackwardsSearch range:NSMakeRange(1, 10)];
22. NSLog(@"%@", NSStringFromRange(range));}
(4) 字符串截取
点击(此处)折叠或打开
1. void subString() {
2. NSString *str = @"123456";
3.
4. // 从索引3开始截取到尾部(包括3)
5. NSLog(@"%@", [str substringFromIndex:3]);
6.
7. // 从头部开始截取到索引3之前(不包括3)
8. NSLog(@"%@", [str substringToIndex:3]);
9.
10. // 指定范围进行截取
11. NSRange range = NSMakeRange(2, 3);
12. NSLog(@"%@", [str substringWithRange:range]);
13.
14. NSString *str2 = @"a-b-c-d-5";
15. NSArray *array = [str2 componentsSeparatedByString:@"-"];
16. NSLog(@"%@", array);
17.
18. NSString *str3 = [array objectAtIndex:0];
19. NSLog(@"%@", str3);
20. }
(5)
#pragma mark
与路径相关
点击(此处)折叠或打开
1. void pathTest() {
2. // 快速创建一个自动释放的数组
3. NSMutableArray *components = [NSMutableArray array];
4. [components addObject:@"Users"];
5. [components addObject:@"MJ"];
6. [components addObject:@"Desktop"];
7. // 将数组中的所有字符串拼接成一个路径
8. NSString *path = [NSString pathWithComponents:components];
9. NSLog(@"%@", path);
10.
11. // 将路径分解成一个数组
12. NSArray *cmps = [path pathComponents];
13. NSLog(@"%@", cmps);
14.
15. // path是一个字符串常量,是不可变的
16. path = @"/users/mj/test";
17. // 判断是够为绝对路径(依据是前面有无/)
18. NSLog(@"%i", [path isAbsolutePath]);
19. NSLog(@"最后一个目录:%@", [path lastPathComponent]);
20. // 删除最后一个目录
21. NSLog(@"%@", [path stringByDeletingLastPathComponent]);
22. // 在最后面拼接一个目录
23. NSLog(@"%@", [path stringByAppendingPathComponent:@"abc"]);
24. }
(6)
#pragma mark
拓展名处理
点击(此处)折叠或打开
1. void extension() {
2. NSString *str = @"/User/MJ/test.txt";
3.
4. NSLog(@"拓展名:%@", [str pathExtension]);
5. // 删除拓展名
6. NSLog(@"%@", [str stringByDeletingPathExtension]);
7. // 添加拓展名
8. NSLog(@"%@", [@"abc" stringByAppendingPathExtension:@"mp3"]);
9.
10. //修改文件名
11. NSString *tmp,*str2 = @"/User/leon/hello.txt";
12. NSString *extensionName =[str2 pathExtension];
13. tmp = [[[[str2 stringByDeletingPathExtension]stringByDeletingLastPathComponent]
14. stringByAppendingPathComponent:@"world"]stringByAppendingPathExtension:extensionName];
15.
16. NSLog(@"%@",tmp);
17. }
(7)
#pragma mark
其他用法,NSString类与基本类型转换等
点击(此处)折叠或打开
1. void other() {
2. NSString *str = @"12";
3. int a = [str intValue];
4. NSLog(@"%i", a);
5.
6. // 计算字数,不是计算字符数
7. NSLog(@"length=%zi", [@"我是字符串123" length]);
8.
9. // 取出对应的字符
10. unichar c = [@"abc" characterAtIndex:0];
11. NSLog(@"%c", c);
12.
13. // 返回C语言中的字符串
14. const char *s = [@"abc" UTF8String];
15. NSLog(@"%s", s);
16. }
NSMutableString:NSString子类,可变字符串
点击(此处)折叠或打开
1. #pragma mark 可变字符串的创建
2. void stringCreate() {
3. // 预先分配10个字数的存储空间
4. NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
5. // 设置字符串内容
6. [str setString:@"1234"];
7.
8. // 拼接一个字符串
9. [str appendString:@"567"];
10. // 拼接字符串
11. [str appendFormat:@"age is %i and height is %.2f", 27, 1.55f];
12.
13. // 替换字符串
14. NSRange range = [str rangeOfString:@"height"];
15. //NSRange range = NSMakeRange(7, 3);
16. [str replaceCharactersInRange:range withString:@"no"];
17.
18. // 插入字符串
19. [str insertString:@"abc" atIndex:2];
20.
21. // 删除字符串
22. range = [str rangeOfString:@"age"];
23. [str deleteCharactersInRange:range];
24.
25. NSLog(@"%@", str);
26.
27. // 释放对象
28. [str release];
29. }