// 字符串转换
char *s = "123";
// 创建字符串,不需要自己释放内存
NSString *str = @"123";
NSString *strr = [[NSString alloc] init];
strr = @"iiii";
// 类型转换
// c -> oc
NSString *str1 = [NSString stringWithUTF8String:s];
NSLog(@"str1 = %@", str1);
// oc -> c
NSLog(@"%s", [str UTF8String]);
// 格式化字符串
int x = 1;
int y = 2;
NSString *str5 = [NSString stringWithFormat: @"a=%d, b=%d", x, y];
// 拼接字符串
NSString *str6 = [str5 stringByAppendingString:str5];
// 大小写转换
NSString *str7 = @"ABCDEF";
NSString *str8 = [str7 lowercaseString];
NSString *str9 = [str8 uppercaseString];
// 前缀和后缀的判断
NSString *str10 = @"www.baidu.com";
BOOL hasPre = [str10 hasPrefix:@"www"];
if(hasPre){
NSLog(@"no");
}
BOOL x1 = [str10 hasSuffix:@".com"];
// 判断两个字符串是否相同
BOOL x2 = [str7 isEqualToString:str8];
// 字符串比较
// NSComparisonResult
// 字符串分割
// 按照指定字符分隔字符串
NSArray *strarry = [str10 componentsSeparatedByString:@","];
// 按照范围截取字符串
NSRange range = NSMakeRange(1, 10);
NSString *str15 = [str10 substringWithRange:range];
// 从某一位截取字符串
NSString *str16 = [str10 substringFromIndex:5];
// 从头开始截取
NSString *str17 = [str10 substringToIndex:5];
// 将字符串拆分为每一个字符
[str15 characterAtIndex:1];
// 查找字符串
// 查找制定字符串的位置
NSRange range1 = [str15 rangeOfString:@"ab"];
// 替换
NSString *str18 = @"1123123123123";
// 替换某一个范围的内容
NSString *str19 = [str18 stringByReplacingCharactersInRange:NSMakeRange(0, 10) withString:@"你好"];
// 用指定字符串替换源字符串的子串
NSString *str20 = [str18 stringByReplacingOccurrencesOfString:@"" withString:@""];
// 读取文件
// 文件来源 1. 本地文件 2. 网络文件
// 路径类
NSString *str21 = @"http://www.baidu.com";
// 网络路径
NSURL *url = [NSURL URLWithString:str21];
// 本地路径
NSURL *fileUrl = [NSURL fileURLWithPath:str21];
NSString *httpstr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(httpstr);
// 读取本地文件
NSString *filstr = [NSString stringWithContentsOfFile:@"" encoding:NSUTF8StringEncoding error:nil];
// 写文件
[str21 writeToFile:@"url" atomically:YES encoding:NSUTF8StringEncoding error:nil];
/*
可变字符串
NSMutableString
可变字符串是字符串的子类
*/
NSMutableString *str22 = [[NSMutableString alloc] initWithCapacity:10];
[str22 setString:@"123"];
// 1.追加字符串
[str22 appendString:@"123"];
[str22 appendFormat:@"%d", 2];
// 2. 替换字符串
NSRange range11 = [str22 rangeOfString:@"31"];
[str22 replaceCharactersInRange:range11 withString:@"aaa"];
// 3. 插入字符串
[str22 insertString:@"a" atIndex:6];
// 4. 删除字符串
NSRange range12 = [str22 rangeOfString:@"123242"];
NSRange range13 = NSMakeRange(1, 2);
[str22 deleteCharactersInRange:range12];