1 #import <Foundation/Foundation.h>
3 int main(int argc, const char * argv[]) {
4 @autoreleasepool {
5
6
7 /*
8 -----------------------------------------------------------
9 字符串的使用
10 ------------------------------------------------------------
11 */
12 //不可变字符串的使用
13
14 //NSString的定义(6种方法)
15
16 NSString *str = @"hello world";
17
18 NSString *str2 = [[NSString alloc] initWithString:str];
19 NSLog(@"%@",str2);
20
21 //用c得字符串创建oc的字符串对象
22 NSString *str3 = [[NSString alloc] initWithUTF8String:"大家好!!!"];
23 NSLog(@"%@",str3);
24
25 //使用格式符拼接一个字符串
26 NSString * str4 = [[NSString alloc] initWithFormat:@"你好 %d %c",5,'b'];
27 NSLog(@"%@",str4);
28
29 //用c得到oc字符串的另一种方法
30 NSString *str5 = [NSString stringWithUTF8String:"你还"];
31 NSLog(@"%@",str5);
32
33 //格式化创建字符串的两种方式
34 NSString * str6 = [NSString stringWithFormat:@"%d %@ %c",1,@"哈喽",'r'];
35 NSLog(@"%@",str6);
36
37 //利用一个字符串创建另一个字符串的第2种方法
38 NSString * str7 = [NSString stringWithString:str];
39 NSLog(@"%@",str7);
40
41 //比较两个字符串的大小以及是否相等
42
43 NSString *s3 = @"456";
44 NSString *s4 = @"456";
45 //方法一
46 //判断两个字符串是否相等 返回为一个BOOL类型的值
47 BOOL zhi = [s3 isEqualToString:s4];
48 NSLog(@"%hhd",zhi);
49 //方法二
50 NSComparisonResult p = [s3 compare:s4];
51 if(p == NSOrderedAscending){
52 NSLog(@"s3<s4");
53 } else if(p == NSOrderedSame) {
54 NSLog(@"s3 == s4");
55 }else if (p ==NSOrderedDescending){
56 NSLog(@"s3>s4");
57 }
58
59 //查找字符在字符串中的位置(返回值为位置和长度)
60 NSString *s5 = @"I am so bad man";
61 NSString *s6 = @"so";
62
63 NSRange range = [s5 rangeOfString:s6];
64 NSLog(@"location:%lu length:%lu",range.location,range.length);
65
66 //获取子字符串,从字符串中提取一部分创建新的字符串(4种方法)
67
68 NSString *s7 = @"0123456789";
69
70 //方法一. 从str中提取从头至第6个字符串(不包括第6个),生成新字符串.返回字符串地址.开区间
71 NSString *s8 = [s7 substringToIndex:6];
72 NSLog(@"%@",s8);
73
74 //方法二. 从str中提取从第6个字符串(包括第6个),到最后一个字符串,生成新字符串,返回字符串地址
75 NSString * s9 = [s7 substringFromIndex:6];
76 NSLog(@"%@",s9);
77
78 //方法三
79 NSRange rang = {3,4};
80 NSString * s10 = [s7 substringWithRange:rang];
81 NSLog(@"%@",s10);
82
83 //方法四
84 NSString *s11 = [s7 substringWithRange:NSMakeRange(3, 4)];
85 NSLog(@"%@",s11);
86
87
88
89 //可变字符串的增删改查 NSMutableString
90 //1.增(append inset)
91
92 NSMutableString *sc = [[NSMutableString alloc]init];
93
94 //设置字符串或添加方法
95 [sc setString:@"hello"];
96 [sc setString:@"world"];
97
98 //添加字符串
99 [sc appendString:@"123"];
100 [sc appendFormat:@"%c",'d'];
101
102 //插入字符串
103 [sc insertString:@"xxxx" atIndex:4];
104 NSLog(@"%@ ",sc);
105
106 //删 (delete)
107 //方法一
108 NSRange ran = NSMakeRange(2, 3);
109 [sc deleteCharactersInRange:ran];
110 //方法二
111 [sc deleteCharactersInRange:NSMakeRange(3, 5)];
112 NSLog(@"%@",sc);
113
114 //替换字符串(replace)(一)*仅适用不可变数组
115 [sc replaceCharactersInRange:NSMakeRange(3, 2) withString:@"iii"];
116 NSLog(@"%@",sc);
117
118 //替换字符串(二)*可变不可变都能用
119 NSString *sb = [sc stringByReplacingOccurrencesOfString:@"34"withString:@"00"];
120 NSLog(@"%@",sb);
121
122
123 /*
124
125 ----------------------------------------------
126
127 NSArray数组的定义和使用
128
129 -----------------------------------------------
130
131 */
132 //不可变数组
133 //定义数组的方法
134 NSArray *c =[NSArray arrayWithObjects:@"12",@"34",@"56",nil];
135 NSLog(@"%@",c);
136
137 //查找数组对应位置元素
138 NSArray *c1 = [c objectAtIndex:2];
139 NSLog(@"%@",c1);
140
141 NSInteger i = [c indexOfObject:@"56"];
142 NSLog(@"%ld",i);
143
144 //输出数组元素个数
145 NSLog(@"%lu",(unsigned long)[c count]);
146
147 /*
148
149 //可变数组定义及使用
150
151 */
152 NSMutableArray *d = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three", nil];
153 NSLog(@"%@",d);
154
155 //增加数组元素
156 [d addObject:@"1"];
157 [d addObject:@"2"];
158 NSLog(@"%@",d);
159
160 //删除数组元素
161
162 //1.删除指定对象
163 [d removeObject:@"one"];
164 NSLog(@"%@",d);
165 //2.删除对应位置元素
166 [d removeObjectAtIndex:1];
167 NSLog(@"%@",d);
168 //3.删除最后一个
169 [d removeLastObject];
170 NSLog(@"%@",d);
171 //4.删除所有
172 [d removeAllObjects];
173 NSLog(@"%@",d);
174
175
176 NSMutableArray *d1 = [NSMutableArray arrayWithObjects:@"12",@"345",@"659", nil];
177
178 //交换元素位置
179 [d1 exchangeObjectAtIndex:0 withObjectAtIndex:1];
180 NSLog(@"%@",d1);
181
182 //替换数组元素
183 [d1 replaceObjectAtIndex:2 withObject:@"__"];
184 NSLog(@"%@",d1);
185
186
187 //快速枚举法遍历数组
188 for(id obj in d1)
189 {
190 NSLog(@"%@",obj);
191
192 }
193
194 //使用i值遍历数组
195 NSInteger lengt = [d1 count];
196 for (int i=0; i<lengt; i++) {
197 NSLog(@"%@",[d1 objectAtIndex:i]);
198 }
199
200 //枚举器法遍历数组
201 NSEnumerator * enumerator = [d1 objectEnumerator];
202 id obj;
203 while (obj = [enumerator nextObject]) {
204 NSLog(@"%@",obj);
205 }
206
207 //逆序枚举遍历数组
208 NSEnumerator *n = [d1 reverseObjectEnumerator];
209 id ob;
210 while (n = [enumerator nextObject]) {
211 NSLog(@"%@",ob);
212 }
213 NSLog(@"%@",d1);
214
215 //注意:使用枚举法时对数组不能修改,用逆序枚举法也可以改
216
217
218
219
220
221
222
223 //给定一个字符串,判断字符串中是否以"png"结尾,如果是就替换成"jpg",如果不是,就拼接".jpg"
224 //方法一
225 NSString *st = @"00000png";
226 NSInteger leng = [st length];
227 NSLog(@"%lu",leng);
228
229 NSString*st1 = [st substringFromIndex:leng-3];
230 NSLog(@"%@",st1);
231
232 if ([st1 isEqualToString: @"png"]){
233 NSString *st2 =
234 [ st stringByReplacingOccurrencesOfString:st1 withString:@"jpg"];
235 NSLog(@"%@",st2);
236 } else {
237 NSString *st3 =[ st stringByAppendingString:@",jpg"];
238 NSLog(@"%@",st3);
239 }
240
241
242 //方法二
243 NSMutableString *c4 = [[NSMutableString alloc]init];
244 [c4 setString:@"000000000png"];
245
246 NSInteger length = [c4 length];
247 NSLog(@"%ld",(long)length);
248
249 if ([[c4 substringFromIndex:length-3] isEqualToString:@"png"])
250 {
251 [c4 replaceCharactersInRange:NSMakeRange(length-3, 3) withString:@"jpg"];
252 NSLog(@"%@",c4);
253
254 } else {
255 [c4 appendString:@".jpg"];
256 NSLog(@"%@",c4);
257 }
258
259
260 //数组与字符串的联合使用 倒序输出”I am a so bad man”
261 NSString *str1 = @"I am a so bad man";
262 NSArray *arry = [str1 componentsSeparatedByString:@" "];
263 NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
264
265 NSEnumerator *enu = [arry reverseObjectEnumerator];
266
267 id j;
268 while (j = [enu nextObject]) {
269 [mutableArray addObject:j];
270 }
271 NSString *newStr = [mutableArray componentsJoinedByString:@"@"];
272 NSLog(@"%@",newStr);
273
274
275 /*
276
277 --------------------------------------
278
279 NSNumber的使用(用于转化进本数据类型)
280
281 ---------------------------------------
282
283 */
284
285 //把基本类型转化为NSNumber
286 NSNumber *intNumber = [[NSNumber alloc] initWithInt:5];
287 NSNumber *floatNumber = [[NSNumber alloc]initWithFloat:5.0f];
288 NSNumber *doubleNumber = [[NSNumber alloc]initWithDouble:4.67];
289 NSNumber *charNumber = [[NSNumber alloc]initWithChar:'A'];
290 NSLog(@"%@ %@ %@ %@",intNumber,floatNumber,doubleNumber,charNumber);
291
292
293 //通过调用还原基本数据类型
294 NSLog(@"%d",[intNumber intValue]);
295 NSLog(@"%f",[floatNumber floatValue]);
296 NSLog(@"%f",[doubleNumber doubleValue]);
297 NSLog(@"%c",[charNumber charValue]);
298
299
300 //比较两个元素是否相等(返回值为1或0)
301 BOOL ret = [intNumber isEqualToNumber:floatNumber];
302 NSLog(@"%d",ret);
303
304 //另一种比较的方法
305 if([intNumber compare:floatNumber] ==NSOrderedAscending){
306 NSLog(@"<");
307 } else if ([intNumber compare:floatNumber] == NSOrderedSame){
308 NSLog(@"=");
309 } else if ([intNumber compare:floatNumber]==NSOrderedDescending){
310 NSLog(@">");
311 }