#pragma mark - 字符串(NSString)
// 1. 创建空字符串,没有意义
// NSString* str1 = [[NSString alloc]init];
// NSString* str2 = @"";
// 2. 赋值
NSString* str3 = @"hello,world!";
// NSString* str4 = [NSString stringWithString:@"hello"];
// NSString* str5 = [[NSString alloc]initWithString:@"world"];
NSLog(@"%@",str3);
// 3. 格式初始化
int number = 10;
NSString* age = @"年龄";
NSString* people = [NSString stringWithFormat:@"%@:%d岁",age,number];
NSLog(@"%@",people);
截取字符串
NSString* s1 = @"hello,world!";
NSString* s2 = [s1 substringToIndex:5];
NSLog(@"%@",s2);
NSString* s3 = [s1 substringFromIndex:6];
NSLog(@"%@",s3);
截取区间 2-5
NSRange range = NSMakeRange(2, 3);
NSString* s4 = [s1 substringWithRange:range];
NSLog(@"%@",s4);
替换字符串
NSString* http = @"www.baidu.com";
NSString* newHttp = [http stringByReplacingCharactersInRange:NSMakeRange(4, 5) withString:@"新浪"];
NSLog(@"%@",newHttp);
newHttp = [http stringByReplacingOccurrencesOfString:@"com" withString:@"cn"];
NSLog(@"%@",newHttp);
从文件中读取内容
NSString* path = [[NSBundle mainBundle]pathForResource:@"username" ofType:@"txt"];
获取工程文件路径
NSLog(@"%@",path);
NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",content);
分解字符串
NSString* ipID = @"192.168.1.1";
NSArray* ips = [ipID componentsSeparatedByString:@"."];
NSLog(@"%@",ips);
查找
NSRange range1 = [ipID rangeOfString:@"168"];
if (range1.location == NSNotFound) {
NSLog(@"没有找到!");
}else{
NSLog(@"包含字符串");
}
比较字符串大小
NSString* s01 = @"hello";
NSString* s02 = @"HELLO";
NSComparisonResult result = [s01 compare:s02];
if (result == NSOrderedAscending) {
NSLog(@"前面的小");
}else if(result == NSOrderedSame){
NSLog(@"一样大");
}else{
NSLog(@"后面的小");
}
判等
if ([s01 isEqualToString:s02]) {
NSLog(@"相同");
} else{
NSLog(@"不相同");
}
忽略大小写
if([s01 caseInsensitiveCompare:s02] == NSOrderedSame){
NSLog(@"一样");
};
大小写转换
NSString* s = @"hello,WOELD!";
NSString* xs = [s lowercaseString];
NSString* bs = [s uppercaseString];
NSLog(@"%@",xs);
NSLog(@"%@",bs);
#pragma mark - 可变字符串(NSMutableString)
创建空的可变字符串
NSMutableString* mstr = [[NSMutableString alloc]init];
//NSMutableString* mstr1 = @"hello,world";
NSMutableString* mstr2 = [NSMutableString stringWithFormat:@"%@",@"hello"];
NSMutableString* mstr3 = [NSMutableString stringWithString:@"world"];
NSLog(@"%@,%@",mstr2,mstr3);
添加字符串
[mstr appendString:@"张飞"];
NSLog(@"%@",mstr);
int number1 = 11;
[mstr appendFormat:@",赵云,%@,%d",@"关羽",number1];
NSLog(@"%@",mstr);
删除数据
[mstr deleteCharactersInRange:NSMakeRange(1, 4)];
NSLog(@"%@",mstr);
#pragma mark - 数组(NSArray)
创建数组,必须是同一类型的数据,而且是引用类型。
NSArray* array = @[@"张飞",@"关羽",@"刘备"];
NSArray* array1 = [NSArray arrayWithObjects:@"马超",@"黄忠", nil];
数组的遍历
// array.count 数组元素个数
for (int i = 0; i<array.count; i++) {
NSString* arrs = array[i];
NSLog(@"%@",arrs);
}
for (NSString* arrs in array) {
NSLog(@"for_in:%@",arrs);
}
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"block:%@",obj);
if (idx == 1) {
*stop = YES;
}
}];
NSEnumerator* enume = [array objectEnumerator];
NSString* obj = nil;
while (obj = [enume nextObject]) {
NSLog(@"enume:%@",obj);
}
数组的使用
bool isExist = [array containsObject:@"张飞"];
if (isExist) {
NSLog(@"包含");
}else{
NSLog(@"没有");
}
NSUInteger index = [array indexOfObject:@"关羽"];
NSLog(@"%ld",(long)index);
NSString* con = [array objectAtIndex:2];
NSLog(@"%@",con);
排序
NSArray* arr = @[@"111",@"333",@"444",@"222"];
// SEL 方法选择器 compare
NSArray* newArray = [arr sortedArrayUsingSelector:@selector(compare:)];
[[newArray mutableCopy] addObject:@"123"];
NSLog(@"%@",newArray);
#pragma mark - 可变数组(NSMutableArray)
空的可变数组
NSMutableArray* marr = [[NSMutableArray alloc]init];
NSMutableArray* marr1 = [NSMutableArray array];
// 2. 数组的使用
增
[marr1 addObject:@"西游记"];
[marr1 addObject:@"红楼梦"];
NSLog(@"%@",marr1);
[marr1 insertObject:@"三国演义" atIndex:1];
for (NSString* msa in marr1) {
NSLog(@"%@",msa);
}
删
[marr1 removeObject:@"西游记"];
for (NSString* msa in marr1) {
NSLog(@"%@",msa);
}
[marr1 removeObjectAtIndex:0];
for (NSString* msa in marr1) {
NSLog(@"%@",msa);
}
[marr1 removeAllObjects];
NSLog(@"%@",marr1);
[[marr1 copy] addObject:@"123"];
NSLog(@"%@",marr1);