数组 字典 字符串 笔记备

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

一.不可变字符串
1.追加字符串:
[newStr stringByAppendingFormat:@"%C",c];

2.字符串分割方法:
[NSString componentsSeparatedByString:@"\n"];

3.字符串提取:
unichar c = [string characterAtIndex:i];提取第i个元素,返回值为unichar
[string substringToIndex:3]; 从头提取到索引
[string substringFromIndex:3];从索引提取到结束
[string substringWithRange:NSMakeRange(0, 4)];指定范围提取

4.读取文件的标准格式:
NSString *content = [NSString stringWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];

5.创建格式化字符串:占位符
NSString * string = [[NSString alloc] initWithFormat:@”I am %d”,age]];

6.C字符串变oc:
[NSString stringWithUTF8String:buff];

7.比较字符串:
[string1  compare:string2];返回值为-1 0 1
不考虑大小写比较字符串
[string caseInsensitiveCompare:string02];

8.大小写转化:
1.[string uppercaseString] 小写转大写
2.[string lowercaseString] 大写转小写
3.[string capitalizedString] 单词首字母大写 其余小写,其他字符保持不变

9.搜索子串:
NSRange range = [string1 rangeOfString:string2]; //NSRange代表一个范围:子串string2 在长串string1中的范围(位置和长度)
NSMakeRange(location,length)创建一个range结构体类型

10.判断前后缀:
[String1 hasPrefix:@"NSString”]返回值BOOL型
[String1 hasSuffix:@".txt"]

11.分割:
[string componentsSeparatedByString:@“@“];//分割字符串返回值是不可变数组类型

二.可变字符串
1.追加字符串:
[mutableString1 appendString:]
[mutableString1 appendFormat:]

2.指定位置插入字符串:
[mutableString1 insertString:@"Hi! " atIndex:0];

3.在指定范围内删除字符串
[mutableString1 deleteCharactersInRange:NSMakeRange(0, 5)];

4.修改字符串
[mutableString1 setString:@"Hello Word!"];

5.替换:
[mutableString1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];


三.不可变数组:
1.获取数组元素
[arr objectAtIndex:1];
也可以写成arr[1];

2.拼接:
[arr componentsJoinedByString:@“\n”];返回值为字符串

3.查询:
[arr containsObject:(id)];返回值为Bool 如果数组arr中有索引返回YES,没有返回NO

四.可变数组:
1.交换
[mutableArr exchangeObjectAtIndex:i withObjectAtIndex:i+1];

2.增加
[mutableArr addobject:@“four”];

3.在指定位置插入一个元素
[mutableArr addObject:@"four"];

4.删除
[mutableArr removeAllObjects];
[mutableArr removeObject:@"one"];
[mutableArr removeObjectAtIndex:0];

5.修改指定索引的元素
[mutableArr replaceObjectAtIndex:1 withObject:@"二"];

6.排序
[mutableArr sortUsingSelector:@selector(排序准则方法名)];

五.不可变可变字典
1.创建字典
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four", nil];//一个value对应一个key
用一个字典初始化一个新的字典
NSDictionary * dict2 = [[NSDictionary alloc] initWithDictionary:dict]

2.用两个数组实例化一个字典对象,两个数组元素要对应成键值对
NSArray *keys = [NSArray arrayWithObjects:@"one",@"two", nil];
NSArray *values = [NSArray arrayWithObjects:@"1",@"2", nil];
[NSDictionary dictionaryWithObjects:values forKeys:keys];

3.获取所有的值
NSArray *newValues = [dict allValues];

4.获取字典所有的key
NSArray *newKeys = [dict allKeys];

5.根据key 找到相应的值
NSString *str = [dict objectForKey:@"three"];
也可以写成dict[@“three”]

六.可变字典
1.增改:
[mDict setObject:@"111" forKey:@"XXX"];//当key不存的时候表示增加键值对
[mDict setObject:@"111" forKey:@"one"];//当key存在的时候表示修改键值对

2.删除
删除指定的键值对:
[mDict removeObjectForKey:@"XXX"];
删除所有键值对
[mDict removeAllObjects];