Objective-C NSString的使用

本文详细介绍了 Objective-C 中 NSString 的核心概念、常见操作和使用技巧,包括字符串创建、拼接、查找、替换以及其在 iOS 开发中的重要作用。通过实例分析,深入探讨了 NSString 对象的不可变性和内存管理策略,帮助开发者更好地理解和掌握这一基本类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//NSString 用法
NSString *str = [[NSString alloc] init];
        
// 字符串的拼接
NSString *strA = @"今天天气晴!";
NSString *strB = @"最高气温:29度 最低气温:25度";
NSString *strAaddB = [strA stringByAppendingString:strB];
NSLog(@"字符串的拼接:%@", strAaddB);
        
// 字符串的格式化
NSString *strFormat = [NSString stringWithFormat:@"相对湿度%d%%",70];
NSLog(@"字符串格式化:%@", strFormat);
        
// 字符串大小写转换
NSString *speak = @"I am a doctor!";
NSLog(speak);
NSLog(@"LOWER:%@", [speak lowercaseString]);
NSLog(@"UPPER:%@", [speak uppercaseString]);
        
 // 判断前后缀
 str = @"http://www.hello2018.cn";
NSLog(str);
BOOL hasPreFix = [str hasPrefix:@"http://"];
BOOL hasSuffer = [str hasSuffix:@".com"];
if (hasPreFix)
	NSLog(@"%@ 是一个http链接", str);
else
	NSLog(@"%@ 是普通文本", str);
if (!hasSuffer)
	NSLog(@"%@ 是中国域名!", str);
else
	NSLog(@"%@ 不是中国域名!", str);
        
// 判断字符串是否相同
NSLog(@"%@ == %@ : %@", strA, strB, [NSString stringWithFormat:[strA isEqualToString:strB] ? @"TRUE" : @"FALSE"]);
NSLog(@"%@ == %@ : %@", @"ABC", @"ABC", [NSString stringWithFormat:[@"ABC" isEqualToString:@"ABC"] ? @"TRUE" : @"FALSE"]);
        
// 分割字符串
NSArray *arrayStr = [str componentsSeparatedByString:@"."];
for (NSString *strT in arrayStr){
	NSLog(@"strT = %@", strT);
}
        
// 按照范围截取字符串
NSRange rangeSub = NSMakeRange(0, 4);
NSLog(@"截取后的字符串:%@", [str substringWithRange:rangeSub]);
NSLog(@"从后往前截取到第5位:%@", [str substringFromIndex:5]);
NSLog(@"从前往后截取到第5位:%@", [str substringToIndex:5]);
        
// 将字符串拆分位每一个字符
for (int i = 0; i < [str length]; ++i){
	NSLog(@"%c", [str characterAtIndex:i]);
}
        
//查找
NSRange rangeFind = [str rangeOfString:@"www"];
NSLog(@"%@ 出现在:%ld 长度:%ld", @"www", rangeFind.location, rangeFind.length);
// 字符串替换某一范围
NSString *replaceStr = @"Hello, world";
NSString *replaceAfterStr = [replaceStr stringByReplacingCharactersInRange:NSMakeRange(0, 5) withString:@"你好"];
NSLog(@"%@ 替换后: %@", replaceStr, replaceAfterStr);
// 替换某一子串
NSString *replaceAfter2Str = [replaceStr stringByReplacingOccurrencesOfString:@"Hello" withString:@"您好"];
NSLog(@"%@ 替换后: %@", replaceStr, replaceAfter2Str);
        
// 读取文件
// 读取网络文件
// 网络路径名
NSString *networkFileURLStr = @"http://www.baidu.com";
// 网络路径
NSURL *httpURL = [NSURL URLWithString:networkFileURLStr];
// 读取文件
NSString *networkFile = [NSString stringWithContentsOfURL:httpURLencoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", networkFile);
        
// 本地文件路径名
NSString *localFileURLStr = @"/Users/xxx/Desktop/filetxt.txt";
// 本地文件路径
NSURL *fileURL = [NSURL fileURLWithPath:localFileURLStr];
// 读取文件
NSString *localFile = [NSString stringWithContentsOfFile:fileURL encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", localFile);
        
// 写入文件
BOOL judgeFile = [strAaddB writeToFile:@"/Users/xxx/Desktop/writeToFile.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (judgeFile)
	NSLog(@"文件写入成功!");
else
	NSLog(@"文件写入失败!");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值