OC ---- 字符串 数组 iOS学习-----细碎知识点总结
NSString *urlString = [NSString stringWithFormat:@"http://www.apple.com"];
// 获取字符串长度
NSLog(@"长度:%lu", [urlString length]);
// 求子字符串
NSLog(@"%@", [urlString substringFromIndex:4]);
NSLog(@"urlString = %@",urlString);
// 拼接字符串
NSLog(@"======%@", [urlString stringByAppendingString:@"1234"]);
// 替换字符串
NSLog(@"--===-===%@", [urlString stringByReplacingOccurrencesOfString:@"www" withString:@"m"]);
// 字符串相等
NSLog(@"-----%d", [urlString isEqualToString:@"http://www.apple.com"]);
// 字符串比较
NSLog(@"=====%ld", [urlString compare:@"h"]);
#pragma mark - NSMutableString:可变字符串
// 拼接字符串
[mutableString appendString:@"2234"]; // 在原有字符串的基础上进行操作
NSLog(@"---%@", mutableString);
// 插入
[mutableString insertString:@"abcd" atIndex:0];
NSLog(@"--==-%@", mutableString);
// 删除
[mutableString deleteCharactersInRange:range];
NSLog(@"--=+====-%@", mutableString);
// 替换
[mutableString replaceCharactersInRange:range withString:@"www"];
NSLog(@"--=====-%@", mutableString);
// 对于NSString来说,操作的方法一般是以stringBy开头,方法有返回值,得到一个新的字符串,对于NSMutableString来说,一般操作的是自身
数组 获取数组中元素的个数, nill后面的元素不算在内
// 获取数组中对应位置的元素
// NSLog(@"====%@", [arry objectAtIndex:10]);
数组越界
NSLog(@"---%@", [arry lastObject]);
NSLog(@"====%@", [arry firstObject]);
#pragma mark - NSMutableArry
// 实例方法创建对象
NSMutableArray *arry1 = [[NSMutableArray alloc] initWithObjects:@"西游记", @"红楼梦", @"三国演义", @"水浒传", nil];
// 使用便利器创建对象
NSMutableArray *arry2 = [NSMutableArray arrayWithObjects:@"赵云", @"马超", nil];
NSMutableArray *arry3 = [NSMutableArray arrayWithArray:arry];
// 添加元素
[arry2 addObject:@"关羽"];
NSLog(@"%@", arry2);
// 插入
[arry2 insertObject:@"刘备" atIndex:0];
// 删除
[arry2 removeObjectAtIndex:3];
// 替换
[arry2 replaceObjectAtIndex:0 withObject:@"孙尚香"];
// 交换两个元素的位置
[arry2 exchangeObjectAtIndex:0 withObjectAtIndex:2];
// 遍历数组里面的元素
for (int i = 0; i < [arry2 count]; i++) {
NSLog(@"---%@", [arry2 objectAtIndex:i]);
}
// 快速枚举
for (NSString *string in arry2) { // 在使用for 。。。in进行快速枚举的过程中不允许改变集合里面元素的个数。
NSLog(@"%@", string);
}
// 字符串截取
NSArray *arry = [@"2015:08:10 17:45:00"componentsSeparatedByString:@":"];
NSLog(@"%@", arry);
#pragma mark - NSNumber: 值对象
NSNumber *intNumber = [NSNumber numberWithInt:20];
NSArray *arry = [NSArray arrayWithObjects:intNumber, nil];
NSLog(@"%@", intNumber);
NSLog(@"%d", [intNumber intValue]); // 把值对象转换为对象
NSNumber *number = [NSNumber numberWithInt:20];
NSLog(@"%p, %p", intNumber, number); // 享元机制,潜在内存泄露
NSRange rang = NSMakeRange(3, 9);
NSValue *value1 = [NSValue valueWithRange:rang];
NSLog(@"value = %@", value1);
NSLog(@"loc = %lu", [value1 rangeValue].location);
char *str = "一二三四";
NSValue *value2 = [NSValue valueWithPointer:str];
NSLog(@"value2 = %@", value2);
NSLog(@"value2 = %p", [value2 pointerValue]);
NSLog(@"----%@", NSStringFromClass([Book class])); // 把类名转换为字符串
NSLog(@"====%@", NSStringFromRange(rang)); // 把结构体转换为字符串