Beyond平君

导航

OC-字符串函数

Object-C作为当前iOS开发的最热门的语言,是iOS开发的先决条件

本博客主要解释了OC中的字符串函数。

 

OC和C语言的区别:

NSString:创建不可变字符串对象的类

NSMutableString:创建可变字符串对象的类

 

OC语言完全兼容C语言

 

OC字符串与C语言字符串区别

1.OC字符串是一个字符串对象,字符串常量需要用@""包含

2.C语言字符串""包含

3.C语言的字符串以字符的ascll码形式存储

4.OC中的字符串以uicode编码(万国码)形式存储

细分:UTF-8(多字节编码,使用最多)  UTF-16  UTF-64

打印字符串的占位符不同:打印oc%@,C语言中用%s

1.字符串的初始化函数:

解释:在OC中每创建一个字符串对象都是需要对该字符串对象进行初始化的,以下代码中简单的介绍了10种对字符串对象初始化的方法,其中分为两种,一种是对象方法初始化(都是以initXXX()命名的方法),一种是类方法初始化( 都是以stringXXX()命名的方法 )。

NSString不可变字符串对象是不能被修改的,它是创建在堆内存中的。在该类中一些字符串操作函数,看似对字符串对象做了修改,其实是系统底层重新生成了一个字符串对象,并修改生成的字符串,所以说:原来的字符串是没有被修改的。

一个简单的记忆方法:凡事以(NSString *)作为返回值的函数都是在底层重新生成了一个NSString对象的。

 1 /* 字符串初始化常用函数汇总  */
 2 int main(int argc, const char * argv[]) {
 3     @autoreleasepool {
 4         //@"hello world"是一个常量字符串对象,存储在常量区,不可以被修改
 5         NSString *str1 = @"hello world";
 6         NSLog(@"str1 = %@",str1);
 7         
 8         
 9         //用对象的方法来创建格式化 一定格式字符串对象
10         NSString *strFormat = [[NSString alloc] initWithFormat:@"%s%d%@" ,"hello",123,@"world"];
11         NSLog(@"strFormat = %@",strFormat);
12         
13         //用类方法创建格式化的字符串对象
14         NSString *strFormat1 = [NSString stringWithFormat:@"%s%d%@","hello",123,@"qianfeng"];
15         NSLog(@"strFormat1 = %@",strFormat1);
16         
17         //用给定的字符串对象创建字符串对象
18         NSString *str4 = @"中国教育";
19         NSString *str5 = [[NSString alloc] initWithString:str4];
20         NSLog(@"str5 = %@",str5);
21         
22         //用C的字符串创建OC的字符串对象:转换了UTF-8存储的编码格式
23         NSString *str6 = [[NSString alloc] initWithUTF8String:"我是C的字符串"];
24         NSLog(@"str6 = %@",str6);
25         
26         //用C的字符串创建OC的字符串对象,并且设置编码格式
27         NSString *str7 = [[NSString alloc] initWithCString:"我是C的字符串" encoding:NSUTF8StringEncoding];
28         NSLog(@"str7 = %@",str7);
29         
30         //通过类方法创建一个空的字符串   ?autorelease:用类方法创建字符串对象是否可以自动释放内存空间
31         NSString *str8 = [NSString string];
32         NSLog(@"str8 = %@",str8);
33         
34         //通过累对象创建一个空的字符串  <===> str8
35         NSString *str9 = [[NSString alloc] init];
36         NSLog(@"str9 = %@",str9);
37         
38         //和initWithString 相对应 : 用一个已经存在的字符串对象来创建一个新的字符串对象
39         NSString *str10 = [NSString stringWithString:str5];
40         NSLog(@"str10 = %@",str10);
41         
42         //和initWithUTF8String相对应
43         NSString *str11 = [NSString stringWithUTF8String:"我是C语言的字符串"];
44         NSLog(@"str11 = %@",str11);
45         
46         //和initWithCString:encoding方法相对应
47         NSString *str12 = [NSString stringWithCString:"Cyu言的字符串" encoding:NSUTF8StringEncoding];
48         NSLog(@"str12 = %@",str12);
49     }
50     return 0;
51 }
View Code

字符串对象初始化总结:

/*

 总共5种常用的字符串初始函数:分别对应了类函数形式和对象函数两种形式

 特别的:指向常量的OC字符串对象

 1.创建一个空的字符串对象

 2.创建一个一定格式的字符串对象

 3.通过一个已经存在字符串对象来创建一个新的字符串对象

 4.通过C语言字符串来创建一个以UTF-8编码格式的字符串对象

 5.通过C语言字符串来创建一个OC的字符串对象,并且可以设置对应的编码格式

 */

 

2.常用的字符串操作函数

  1 /* 字符串常用操作函数  */
  2 int main(int args,const char *argv[])
  3 {
  4     @autoreleasepool {
  5         //NSString
  6         //创建一个不可变的字符串对象
  7         //NSMutableString
  8         //创建可变字符串对象的类
  9         
 10         //求字符串长度
 11         NSString *str1 = @"hello world 前锋";
 12         NSUInteger length = [str1 length]; //typedef unsigned long NSUInteger;
 13         NSLog(@"length = %lu",length);
 14         
 15         //获取字符串指定位置的字符
 16         unichar ch = [str1 characterAtIndex:1];  //unichar : unsigned short
 17         NSLog(@"ch = %C",ch);//%C打印unichar字符 %c打印ASCLL字符
 18         
 19         
 20         /*
 21             子字符串提取方法
 22          */
 23         //从下标位置开始提取子串,到字符串末尾
 24         NSString *subStr1 = [str1 substringFromIndex:12];
 25         NSLog(@"subStr1 = %@",subStr1);
 26         
 27         //提取字符串从开始位置到指定位置(不包括指定下标)
 28         NSString *subToStr1 = [str1 substringToIndex:12];
 29         NSLog(@"sbuToStr1 = %@",subToStr1);
 30         
 31         
 32         //NSRange是一个结构体,结构体中存放了两个变量,分别是location:字符串开始位置,length:提取长度 (这样就构成了一个提取字符串的范围)
 33         NSRange range = {6,5};
 34         NSString *subRangeStr = [str1 substringWithRange:range];
 35         NSLog(@"subRangeStr = %@",subRangeStr);
 36         
 37         //NSMakeRange(location,length) 返回一个结构体变量
 38         NSString *subRangeStr1 = [str1 substringWithRange:NSMakeRange(2, 12)];
 39         NSLog(@"subRangeStr1 = %@",subRangeStr1);
 40         
 41         
 42         /*
 43             字符串比较函数
 44          */
 45         NSString *str2 = [NSString stringWithCString:"hello world" encoding:NSUTF8StringEncoding];
 46         NSString *str3 = [NSString stringWithUTF8String:"helLo world"];
 47         NSComparisonResult result = [str2 compare:str3];
 48         if (result == NSOrderedAscending) {
 49             NSLog(@"str2 < str3");
 50         }else if (result == NSOrderedSame)
 51         {
 52             NSLog(@"str2 = str3");
 53         }else{
 54             NSLog(@"str2 > str3");
 55         }
 56         
 57         //caseInsensitiveCompare:以大小写不敏感的方式比较字符串
 58         //NSComparisonResult:是一个枚举类型
 59         NSComparisonResult res2 = [str2 caseInsensitiveCompare:str3];
 60         if (res2 == NSOrderedSame) {
 61             NSLog(@"str2 = str3");
 62         }else if(res2 == NSOrderedAscending)
 63         {
 64             NSLog(@"str2 < str3");
 65         }else{
 66             NSLog(@"str2 > str3");
 67         }
 68         
 69         /*
 70             - (BOOL)isEqualToString:(NSString *)aString;
 71          
 72             - (BOOL)hasPrefix:(NSString *)aString;
 73             - (BOOL)hasSuffix:(NSString *)aString;
 74          */
 75         BOOL ret = [str2 isEqualToString:str3];
 76         if (ret == YES) {
 77             NSLog(@"isEqualToString str2 = str3");
 78         }else{
 79             NSLog(@"isEqualToString str2 != str3");
 80         }
 81         
 82         //判断前缀子串
 83         BOOL ret1 = [@"www.baidu.com" hasPrefix:@"www"];
 84         NSLog(@"ret1 = %d",ret1);
 85         //判断后缀子串
 86         BOOL ret2 = [@"www.baidu.com" hasSuffix:@".com"];
 87         NSLog(@"ret2 = %d",ret2);
 88         
 89         //是否包含子串(10.10mac os)
 90         bool ret3 = [@"www.baidu.com" containsString:@"baidu"];
 91         NSLog(@"ret3 = %d",ret3);
 92         
 93         
 94         
 95         //查找子串
 96         //如果不能查找到子串,则返回一个long类型的最大的值
 97         //rangeOfString:是从左往右进行查找,
 98         NSString *str4 = [[NSString alloc] initWithFormat:@"%s","hello world qianworldfeng"];
 99         NSRange range1 = [str4 rangeOfString:@"world"];
100         if (range1.location == NSNotFound) {
101             NSLog(@"没有查找到子串 notfound = %lu",NSNotFound);
102         }else{
103             NSLog(@"location = %lu length = %lu",range1.location,range1.length);
104         }
105         
106         //倒序查找子串
107         NSRange range2 = [str4 rangeOfString:@"world"  options:NSBackwardsSearch];
108         NSLog(@"location = %lu length = %lu",range2.location,range2.length);
109         
110         
111         /*
112             凡事返回值为 NSSring * 的,底层都是创建了一个新的对象
113          */
114         
115         //字符串追加
116         NSString *str5 = @"hello"; //常量字符串是不能修改的
117         NSLog(@"地址 str5 = %p",str5);
118         //不是在原来str5上追加的,而是在底层创建了一个新的字符串
119         str5 = [str5 stringByAppendingString:@" world"]; //创建了一个新的字符串对象
120         NSLog(@"新地址 str5 = %p",str5);
121         NSLog(@"str5 = %@",str5);
122         
123         //格式化追加字符串
124         NSString *str6 = @"qianfeng";
125         str6 = [str6 stringByAppendingFormat:@"%d%s",123,"hello world"];
126         NSLog(@"str6 = %@",str6);
127         
128         
129         //把字符串对象转换成相应的类型对象 例如:字符串类型转称int
130         int a = [@"123" intValue];
131         float f = [@"123.123" floatValue];
132         NSLog(@"a = %d f = %.3f",a,f);
133         
134         
135         //返回公共前缀子串
136         NSString *str7 = [@"www.baidu.com" commonPrefixWithString:@"www.hao123.com" options:NSLiteralSearch];
137         NSLog(@"str7 = %@",str7);
138         
139         
140         //大小写转换
141         NSString *str8 = [@"baidu" uppercaseString]; //小写转大写
142         NSLog(@"str8 = %@",str8);
143         NSString *str9 = [@"BAIdu" lowercaseString]; //大写转小写
144         NSLog(@"str9 = %@",str9);
145         NSString *str10 = [@"bai du" capitalizedString]; //每个单词的首字母转换成大写
146         NSLog(@"str10 = %@",str10);
147     
148         
149         //字符串替换
150         NSString *str11 = [@"baidu qianfengqian" stringByReplacingOccurrencesOfString:@"qian" withString:@"QIAN"];
151         NSLog(@"str11 = %@",str11);
152     
153         //将OC的字符串对象转换成C语言的字符串
154         NSLog(@"我是C字符串:%s",[@"woshiduixiang" UTF8String]);
155         
156         
157         //替换指定范围内的字符 重要
158         NSString *str12 = @"hello world qianfeng";
159         str12 = [str12 stringByReplacingCharactersInRange:NSMakeRange(12, 8) withString:@"welcome"];
160         NSLog(@"str12 = %@",str12);
161         
162         
163         //利用一个网址内的所有内容生成一个OC的字符串对象
164         NSURL *url = [[NSURL alloc]initWithString:@"https://www.baidu.com/"];
165         //利用网址
166         NSString *urlContent = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; //nil空指针
167         NSLog(@"urlContent = %@",urlContent);
168         
169         NSString *fileContent = [NSString stringWithContentsOfFile:@"/Users/qianfeng/Desktop/Calculator/Calculator/Calculator.c" encoding:NSUTF8StringEncoding error:nil];
170         NSLog(@"fileContent = %@",fileContent);
171         
172     }
173 }
View Code

 

3.可变字符串NSMutableString是继承于不可变字符串NSString的,所以NSSting类中的所有函数,NSMutableString都是可以使用的。

NSMutableString类中所独有的几个操作函数

 1 int main(int args,const char *argv[])
 2 {
 3     @autoreleasepool {
 4         
 5         //- (NSMutableString *)initWithCapacity:(NSUInteger)capacity;
 6         //+ (NSMutableString *)stringWithCapacity:(NSUInteger)capacity;
 7         //创建一个可变字符串对象 并初始化大小位20个字节
 8         NSMutableString *mulStr1 = [[NSMutableString alloc]initWithCapacity:20];
 9         NSLog(@"mulStr1 = %@",mulStr1);
10         
11         
12         NSMutableString *mulStr2 = [[NSMutableString alloc]initWithString:@"hello world qianfeng"];
13         [mulStr2 replaceCharactersInRange:NSMakeRange(6, 5) withString:@"welcome"];
14         NSLog(@"mulStr2 = %@",mulStr2);
15         
16         
17         //在指定位置增加字符串
18         NSMutableString *mulStr3 = [[NSMutableString alloc]initWithString:@"前锋中国"];
19         [mulStr3 insertString:@"hello" atIndex:2];
20         NSLog(@"mulStr3 = %@",mulStr3);
21         
22         //删除指定范围的字符
23         NSMutableString *mulStr4 = [[NSMutableString alloc]initWithString:@"前锋中国"];
24         [mulStr4 deleteCharactersInRange:NSMakeRange(1, 2)];
25         NSLog(@"mulStr4 = %@",mulStr4);
26         
27         
28         //追加字符串
29         NSMutableString *mulStr5 = [[NSMutableString alloc]initWithUTF8String:"woshiC字符串"];
30         [mulStr5 appendString:@"我是zhuijia的"];
31         NSLog(@"mulStr5 = %@",mulStr5);
32         
33         //格式化追加字符串
34         NSMutableString *mulStr6 = [NSMutableString stringWithString:@"我是dobi"];
35         [mulStr6 appendFormat:@"%s%d","123",999];
36         NSLog(@"mulStr6 = %@",mulStr6);
37         
38         //重置字符串
39         NSMutableString *mulStr7 = [NSMutableString stringWithFormat:@"%s%d","haha",998];
40         [mulStr7 setString:@"我是更改的"];
41         NSLog(@"mulStr7 = %@",mulStr7);
42     }
43 }
View Code

 

posted on 2015-06-11 09:03  Beyond平君  阅读(378)  评论(0编辑  收藏  举报