标签:
1 string:创建一个空字符串
1 / NSString
2 // NSString *string1 = [NSString stringWithString:@"appbear"];
3 // NSLog(@"%@",string1);
4
5
6 NSString *string2 = @"bearapp";
7 NSLog(@"%@",string2);
8
9
10 NSString *string3 = [NSString stringWithFormat:@"today is %d",5];
11 NSLog(@"%@",string3);
12
13 // 连接两个字符串
14 NSString *string4 = [NSString stringWithFormat:@"today is %@",string2];
15 NSLog(@"%@",string4);
16
17
18 // 判断string是不是一样
19 NSString *codeString = @"cocochina123";
20 NSString *codeString2 = @"cocochina123";
21
22 if ([codeString isEqualToString:codeString2]) {
23 NSLog(@"两个字符串相等");
24 }else{
25 NSLog(@"两个字符串不相等");
26 }
27
28 // 比较地址是否相同
29 if (codeString2==codeString) {
30 NSLog(@"==");
31 }else{
32 NSLog(@"!=");
33 }
34
35 // 大小写转换
36 // 全部转化为大写
37 NSString *up = [codeString uppercaseString];
38 NSLog(@"%@",up);
39 // 全部转化为小写
40 NSString *low = [codeString2 lowercaseString];
41 NSLog(@"%@",low);
42 // 全部首字母大写
43 NSString *cap = [string4 capitalizedString];
44 NSLog(@"%@",cap);
45
46
47
48 NSString *stri1 = @"abcdefg";
49 NSString *stri2 = @"bbcdefg";
50 if (stri1==stri2) {
51 NSLog(@"两个字符串相同");
52 }else{
53 NSLog(@"两个字符串不同");
54 }
55 if ([stri1 isEqualToString:stri2]) {
56 NSLog(@"两个字符串equal");
57 }else{
58 NSLog(@"两个字符串not equal");
59 }
60 // NSComparisonResult是一个枚举量
61 NSComparisonResult resut = [stri1 caseInsensitiveCompare:stri2];
62
63 NSLog(@"%ld",resut);
64 */
65 // 截取字符串
66 NSString *string1 = @" 今 天 天晴 了 ";
67 NSString *string2 = @" 今天 天阴 了 ";
68 NSLog(@"%@",[string1 substringFromIndex:2]);
69 NSLog(@"%@",[string2 substringToIndex:2]);
70
71 NSRange range = {1,3};
72 NSLog(@"%@",[string1 substringWithRange:range]);
73
74 // 拼接两个字符串
75 NSString * lastOutString = [string1 stringByAppendingString:string2];
76 NSLog(@"%@",lastOutString);
77
78 // 去掉字符串两头的空格
79 NSString *lastTrimString = [string1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
80 NSLog(@"%@",lastTrimString);
81
82
83 // 替换字符串的内容
84 NSString *replace = [string1 stringByReplacingOccurrencesOfString:@"今天" withString:@"昨天"];
85 NSLog(@"%@",replace);
86
87
88 // 搜索字符串第一种方法
89 NSRange range1 = [string1 rangeOfString:@"天晴"];
90 if (range1.location!=NSNotFound) {
91 NSLog(@"找到天晴了");
92 }else{
93 NSLog(@"没找到天晴");
94 }
95
96 // 搜索字符串第二种方法
97 NSRange range2 = [string1 rangeOfString:@"天晴"];
98 if (range2.length!=0) {
99 NSLog(@"找到天晴了%lu,%lu",(unsigned long)range2.location,(unsigned long)range2.length);
100 }else{
101 NSLog(@"没找到天晴");
102 }
标签:
原文地址:http://www.cnblogs.com/jiwangbujiu/p/5309024.html