pragma mark NSMutableString练习
pragma mark 概念
/**
字符串 拼接 (stringByAppendingString)
删除字符串的指定范围 并且以什么代替 (stringByReplacingCharactersInRange:withString)
去除字符串的首尾字符 (stringByTrimmingCharactersInSet) 空格等
删除一个范围 (deleteCharactersInRange)
*/
pragma mark 代码
#import <Foundation/Foundation.h>
#pragma mark 类
#pragma mark main函数
int main(int argc, const char * argv[])
{
#pragma mark NSMutableString练习
/**
需求: 将3个520lyh 拼接在一起, 中间用空格隔开
520lyh 520lyh 520lyh
*/
#warning 1.不可变字符串 拼接
/*
NSString *subStr = @"520lyh";
// 520lyh
NSString *newStr = [subStr stringByAppendingString:@" "];
// 520lyh 520lyh
newStr = [newStr stringByAppendingString:subStr];
// 520lyh 520lyh_
newStr = [newStr stringByAppendingString:@" "];
// 520lyh 520lyh 520lyh
newStr = [newStr stringByAppendingString:subStr];
NSLog(@"newStr = |%@|",newStr);
newStr = nil;
*/
#warning 1.1 循环 不可变字符串 拼接
NSString *subStr = @"520lyh";
NSString *newStr = [subStr stringByAppendingString:@" "];
// NSString *newStr = [subStr stringByAppendingString:@" "];
// 循环操作
#warning -- 注意⚠️: 在开发中 如果需要对字符串进行频繁的操作,不要使用不可变的字符串
for (int i = 0; i < 2; ++i) {
// 520lyh 520lyh
newStr = [newStr stringByAppendingString:subStr];
newStr = [newStr stringByAppendingString:@" "];
}
#warning 2.删除字符串的指定范围 (删除最后一个 获取长度-1) (stringByReplacingCharactersInRange:withString)
// newStr = [newStr stringByReplacingCharactersInRange:NSMakeRange(newStr.length - 1, 1) withString:@""];
newStr = [newStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; // 这里只会去除末尾的空格
NSLog(@"newStr = |%@|",newStr);
#pragma mark 可变字符串
#warning 3.可变字符串
subStr = @"520lyh";
NSMutableString *strM = [NSMutableString string];
for (int i = 0; i < 3; ++i)
{
// 1. 添加一个520lyh
[strM appendString:subStr];
// 2.添加一个空格
[strM appendString:@" "];
}
#warning 4.删除一个范围 (deleteCharactersInRange)
[strM deleteCharactersInRange:NSMakeRange(strM.length - 1, 1)];
NSLog(@"strM = |%@|",strM);
return 0;
}
网友评论