主要分为两个部分1. NSString 2. NSMutableString
一. NSString
char* s = "Hello";
NSString* str = @"Hello";
// OC, C 字符串转换
// C->OC
NSString* str1 = [NSString stringWithUTF8String:s];
NSLog(str1);
// OC->C
NSLog(@"str2 = %s", [str UTF8String]);
// 创建字符串
NSString* str3 = @"iOS"; // 相当于系统自动给你分配内存不需要手动释放
NSString* str4 =[[NSString alloc] init]; // 需要手动释放
str4 = @"iOS";
// 格式化字符串
int a = 10;
int b = 20;
NSString* str5 = [NSString stringWithFormat:@"a= %d b = %d", a, b];
NSLog(@"str5 = %@", str5);
// 拼接字符串
NSString* str6 = [str5 stringByAppendingString:str3];
NSLog(@"str6 = %@", str6);
// 大小写转换
NSString* str7 = @"aBcdEF";
NSString* str8 = [str7 lowercaseString];
NSLog(@"str8 = %@", str8);
NSString* str9 = [str7 uppercaseString];
NSLog(@"str9 = %@", str9);
//前缀和后缀的判断
NSString* str10 = @"www.imooc.com";
//判断前缀
BOOL hasPrefix = [str10 hasPrefix:@"www."];
if(hasPrefix){
NSLog(@"有对应前缀");
}
else{
NSLog(@"没有对应前缀");
}
//判断后缀
BOOL hasSuffix = [str10 hasSuffix:@".com"];
if(hasSuffix){
NSLog(@"有对应后缀");
}
else{
NSLog(@"没有对应后缀");
}
// 判断字符串相同
NSString* str11 = @"Hello";
NSString* str12 = @"hello";
if([str11 isEqualToString:str12]){
NSLog(@"两个字符串一致");
}
else{
NSLog(@"两个字符串不一致");
}
// 分割字符串
// 按照指定字符分隔字符穿
NSString* str13 = @"a,b,c,d,e";
NSArray* strArray = [str13 componentsSeparatedByString:@","];
for(NSString* str in strArray){
NSLog(@"str = %@", str);
}
// 按照范围截取字符串
NSRange range = NSMakeRange(1, 5); // NSRange 不是一个类,而是一个struct,所以不是指针
NSString* str14 = [str13 substringWithRange:range];
NSLog(@"str14 = %@", str14);
// 从某一位开始截取后面的字符串
NSString* str15 = [str13 substringFromIndex:2];
NSLog(@"str15 = %@", str15);
// 从开头截取到某一位
NSString* str16 = [str13 substringToIndex:5];
NSLog(@"str16 = %@", str16);
// 将字符串拆分位每一个字符
for(int i = 0; i < [str13 length]; i++){
NSLog(@"char[%d] is %c", i, [str13 characterAtIndex:i]);
}
// 查找字符串
NSString* str17 = @"ab cd ef gh ij";
// 查找指定字符串的位置
NSRange range1 = [str17 rangeOfString:@"ab"];
NSLog(@"range1.location is %d, rang1.length is %d ", range1.location, range1.length);
// 替换
NSString* str18 = @"Hello iOS, Hello Duke";
//替换某一部分内容
NSString* str19 = [str18 stringByReplacingCharactersInRange:NSMakeRange(0, 5) withString:@"你好"];
NSLog(@"Str19 = %@", str19);
// 用指定字符串替换原字符串中的字符串
NSString* str20 = [str18 stringByReplacingOccurrencesOfString:@"Hello" withString:@"你好"];
NSLog(@"Str20 = %@", str20);
// 读取文件
// 文件来源,1. 本地文件 2. 网路文件
// 路径类
NSString* str21 = @"www.baidu.com";
// 网络路径
NSURL* httpurl = [NSURL URLWithString:str21];
// 本地路径
NSURL* fileurl = [NSURL fileURLWithPath:@"/Users/lumingming/Desktop/test.txt"];
//读取网络文件
NSString* httpStr = [NSString stringWithContentsOfURL:httpurl encoding:NSUTF8StringEncoding error:nil];
NSLog(@"httpStr = %@", httpStr);
NSString* fileStr = [NSString stringWithContentsOfURL:fileurl encoding:NSUTF8StringEncoding error:nil];
NSLog(@"fileStr = %@", fileStr);
// 写入文件
NSString* str22 = @"Hello Fred";
[str22 writeToFile:@"/Users/lumingming/Desktop/Demo.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
二. NSMutableString
// 可变字符串是字符串的子类
NSMutableString* str = [[NSMutableString alloc] initWithCapacity:10];
[str setString:@"Hello"];
//str = @"HEllo"; //左边这种写法不正确,这是NSString的赋值方法,不适用于NSMUtableString
// 1. 追加字符串
[str appendString:@", World"];
NSLog(@"Str is %@", str);
int a = 10;
// 2. 追加格式化字符串
[str appendFormat:@"%d", a];
NSLog(@"Str is %@", str);
// 3. 替换字符串
NSRange range = [str rangeOfString:@"World"];
[str replaceCharactersInRange:range withString:@"Fred"];
NSLog(@"Str is %@", str);
// 4. 插入字符串
[str insertString:@"Android" atIndex:6];
NSLog(@"Str is %@", str);
// 5. 删除字符串
NSRange range1 = [str rangeOfString:@"Fred"];
[str deleteCharactersInRange:range1];
NSLog(@"Str is %@", str);