// NSString
//代开API文档
//Xcode -> help - Documentation and API Reference
//快速打开API
//alt(option) + 鼠标左键
//快速进入头文件
//command + 鼠标左键
// NSString是oc中的不可变字符串类,被创建后,不能修改
//常用方法
//1.创建字符串
NSString *str1 = @"iPhone";
NSLog(@"%@", str1);
NSString *str2 = [[NSString alloc] init];
NSLog(@"%@", str2);
NSString *str3 = [[NSString alloc] initWithString:@"iPhone"];
NSLog(@"%@", str3);
// NSString *str4 = [[NSString alloc] initWithString:nil];
// NSLog(@"%@", str4); error
NSString *str5 = [[NSString alloc] initWithFormat:@"%@5s", str1];
NSLog(@"%@", str5);
// NSString *str6 = [NSString stringWithString:@"iPhone"];
// NSLog(@"%@", str6);
NSString *str7 = [NSString stringWithFormat:@"123"];
NSLog(@"%@", str7);
//2.获取字符串长度
NSUInteger length = [str7 length];
NSLog(@"%lu", length);
//3.判断字符串是否以指定字符串开始或结束
NSString *str8 = @"123ABC";
BOOL result = [str8 hasPrefix:@"223"];
NSLog(@"%d", result);
BOOL result1 = [str8 hasSuffix:@"C"];
NSLog(@"%d", result1);
//4.搜索字符串的范围
NSRange range = [str8 rangeOfString:@"3A"];
NSLog(@"%lu, %lu", range.length, range.location);
//5.字符串截取
NSRange rs = {3, 3};
NSString *str9 = [str8 substringWithRange:rs];
NSLog(@"%@", str9);
//6.拼接字符串
NSString *str10 = [str8 stringByAppendingString:@"123"];
NSLog(@"%@", str8);
NSLog(@"%@", str10);
//7.替换字符串
NSString *str11 = [str8 stringByReplacingOccurrencesOfString:@"3A" withString:@"A"];
NSLog(@"%@", str11);
//8.字符串比较"123" "123" "321"
NSInteger flag = [@"123" compare:@"123"];
NSLog(@"%ld", flag);
//9.字符串和数值类型的转换
NSString *str12 = @"123";
int a = [str12 intValue];
NSLog(@"%d", a);
double b = [str12 doubleValue];
NSLog(@"%f", b);
float c = [str12 floatValue];
NSLog(@"%f", c);
BOOL e = [str12 boolValue];
NSLog(@"%d", e);
//10.大小写转换操作
NSString *aaa = @"aBcE";
NSLog(@"%@", [aaa capitalizedString]);
NSLog(@"%@", [aaa uppercaseString]);
NSLog(@"%@", [aaa lowercaseString]);
NSLog(@"%@", aaa);
//NSMutableString
//capctiy 是一个预估的值,可以改变
//由于内存比较紧张,一般把capctiy设置成0,让字符串自己去判断大小,扩充容器大小
//创建一个新的字符串
NSMutableString *mString = [[NSMutableString alloc] initWithCapacity:0];
//拼接字符串
[mString appendString:@"taiyang"];
NSLog(@"%@", mString);
//插入字符
[mString insertString:@"aaa" atIndex:1];
NSLog(@"%@", mString);
//删除字符串
// NSRange r = {2, 3};
// [mString deleteCharactersInRange:r];
// || 等价
[mString deleteCharactersInRange
练习 :
// 1.截取字符串“20|http://www.baidu.com”中 “|” 前面和后面的字符串,并输出。
// NSString *str1 = @"20|http://www.baidu.com";
// 1
// NSString *str2 = [str1 substringWithRange:NSMakeRange(0, 2)];
// NSString *str3 = [str1 substringWithRange:NSMakeRange(3, 20)];
// NSLog(@"str2 = %@ str3 = %@", str2, str3);
//2
// NSString *str2 = [str1 substringFromIndex:3];
// NSLog(@"str2 = ‘%@‘", str2);
// NSString *str3 = [str1 substringToIndex:2];
// NSLog(@"str3 = ‘%@‘", str3);
// 2.将“文艺青年”改成“213青年”。
// NSString *str1 = @"文艺青年";
// NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"文艺" withString:@"213"];
// NSLog(@"%@", str2);
// 3.给定一个图片文件名,判断字符串中是否有后缀,如果有(如:以“.png”结尾),就替换成“jpg”;如果没有,就拼接”.jpg”
// NSString *str1 = @"taiyang";
// NSUInteger length = [str1 length];
// NSString *str2 = [str1 substringFromIndex:length - 4];
// if ([@".jpg" compare:str2] == 0) {
// NSLog(@"该字符串是以.jpg开头");
// }else if ([@".png" compare:str2] == 0){
// NSString *str3 = [str1 stringByReplacingOccurrencesOfString:@".png" withString:@".jpg"];
// NSLog(@"str3 = ‘%@‘", str3);
// }else{
// NSString *str4 = [str1 stringByAppendingString:@".jpg"];
// NSLog(@"str4 = ‘%@‘", str4);
// }