版权声明:本文为延成原创文章,转载请标明出处
文件读取
NSError *error = nil;
NSString *path = @"/Users/releasemr./Desktop/content.txt";
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
NSLog(@"str == %@",str);
}else{
NSLog(@"error = %@",[error localizedDescription]);
}
NSString *path2 = @"file:///Users/releasemr./Desktop/content.txt";
//如果path中有中文
path2 = [path2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:path2];
NSURL *url2 = [NSURL fileURLWithPath:@"/Users/releasemr./Desktop/content.txt"];
NSString *str2 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str2 == %@",str2);
//文件写入
NSString *str3 = @"hello world33";
NSString *path3 = @"/Users/releasemr./Desktop/content2.txt";
BOOL flag = [str3 writeToFile:path3 atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"flag == %i",flag);
NSString * path33 = @"file:///Users/releasemr./Desktop/content2.txt";
// NSString *url3 = [NSURL URLWithString:path33];
NSString *url3 = [NSURL fileURLWithPath:path33];
[str3 writeToURL:url3 atomically:YES encoding:NSUTF8StringEncoding error:nil];
字符串比较
NSString *str1 = @"abc";
NSString *str2 = @"ABC";
//比较的是内容
BOOL flag = [str1 isEqualToString:str2];
NSLog(@"flag == %i",flag);
//比较的是地址
flag = (str1 == str2);
NSLog(@"flag == %i",flag);
switch ([str1 caseInsensitiveCompare:str2]) {
case NSOrderedAscending:
NSLog(@"str1小于str2");
break;
case NSOrderedSame:
NSLog(@"str1等于str2");
break;
case NSOrderedDescending:
NSLog(@"str1大于str2");
break;
default:
break;
}
字符串搜索
NSString *str = @"https://www.baidu.com";
//判断是否以什么开头
if ([str hasPrefix:@"https"]) {
NSLog(@"是一个URL");
}else{
NSLog(@"不是一个URL");
}
//判断是否以什么结尾
if ([str hasSuffix:@".com"]) {
NSLog(@"是以.com结尾");
}else{
NSLog(@"不是以.com结尾");
}
//判断是否包含baidu
NSRange range = [str rangeOfString:@"baidu"];
// if (range.location == NSNotFound) {
if (range.location == 0) {
NSLog(@"不包含baidu");
}else{
NSLog(@"location == %lu,length == %lu",range.location,range.length);
}
字符串截取
NSString *str = @"<head>百度</head>";
NSUInteger loaction = [str rangeOfString:@">"].location + 1;
NSUInteger length = [str rangeOfString:@"</"].location - loaction;
NSRange range = NSMakeRange(loaction, length);
NSString *newStr = [str substringWithRange:range];
NSLog(@"newStr == %@",newStr);
//从指定位置截取,一直截到末尾
NSString *str2 = [str substringFromIndex:6];
NSLog(@"str2 == %@",str2);
//从开头开始截,一直截到什么位置
NSString *str3 = [str substringToIndex:6];
NSLog(@"str3 == %@",str3);
字符串替换
NSString *str = @"https://www.baidu.com";
NSString *newStr = [str stringByReplacingOccurrencesOfString:@"baidu" withString:@"ali"];
NSLog(@"newStr == %@",newStr);
//去空格
NSString *str2 = @"https ://www. baidu. com";
NSString *newStr2 = [str2 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"newStr2 == %@",newStr2);
//替换首位
NSString *str3 = @" https://www.baidu.com ";
NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
NSString *newStr3 = [str3 stringByTrimmingCharactersInSet:set];
NSLog(@"newStr3 == %@",newStr3);
字符串和路径
NSString *str = @"/Users/releasemr./Desktop/content.txt";
if ([str isAbsolutePath]) {
NSLog(@"是绝对路径");
}else{
NSLog(@"不是绝对路径");
}
//获取文件路径中的最后一个目录
NSString *newStr = [str lastPathComponent];
NSLog(@"newStr == %@",newStr);
//给文件路径添加一个目录
NSString *newStr2 = [str stringByAppendingPathComponent:@"xml"];
NSLog(@"newStr2 == %@",newStr2);
//删除文件路径中的最后一个目录
NSString *newStr3 = [str stringByDeletingLastPathComponent];
NSLog(@"newStr3 == %@",newStr3);
//获取文件路径的扩展名
newStr = [str pathExtension];
NSLog(@"newStr == %@",newStr);
//删除文件路径的扩展名
newStr = [str stringByDeletingPathExtension];
NSLog(@"newStr == %@",newStr);
//给文件路径添加一个扩展名
newStr = [str stringByAppendingPathExtension:@"jpg"];
NSLog(@"newStr == %@",newStr);
字符串转换
//将字符串转换为大写
NSString *str = @"aBc";
NSString *newStr = [str uppercaseString];
NSLog(@"newStr == %@",newStr);
//将字符串转换为小写
newStr = [str lowercaseString];
NSLog(@"newStr == %@",newStr);
//将字符串首字母转换为大写
newStr = [str capitalizedString];
NSLog(@"newStr == %@",newStr);
//字符串与基本数据类型的转换
NSString *str1 = @"110";
NSString *str2 = @"119";
int value1 = [str1 intValue];
int value2 = [str2 intValue];
NSLog(@"sum == %i", value1 + value2);
//c语言与oc字符串之间的转换
char *cStr = "baidu";
NSString *str3 = [NSString stringWithUTF8String:cStr];
NSLog(@"str == %@",str3);
NSString *newStr3 = @"baidu";
const char *cStr2 = [newStr3 UTF8String];
NSLog(@"str == %s",cStr2);
延成
发布了139 篇原创文章 · 获赞 19 · 访问量 2万+
私信
关注