1.去掉左右两边的空格;
NSString *str = @" 正文内容,头部尾部带有空格 ";
NSString *dealtStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
2.替换某个字符串中的所有符合条件的字符
NSString *str = @" 正文内容,头部尾部带有空格 ";
NSString *dealtStr = [str stringByReplacingOccurrencesOfString:@" " withString:@"XXX"];
//NSLog : XXX正文内容,头部尾部带有空格XXX
3..截取字符串
NSString*string =@"www.ABCDEFGHIJKLMN.com.cn";
//取0~第7-1个
NSString *string1 = [string substringToIndex:7];
NSLog(@"%@",string1);
//取第7个~结尾
NSString *string2 = [string substringFromIndex:7];
NSLog(@"%@",string2);
//取第4个后面的长度为2的字符
NSString *string3 = [string substringWithRange:NSMakeRange(4, 2)];
NSLog(@"%@",string3);
//输出:
2016-08-08 16:49:24.061 宏定义的黑魔法[4125:213926] www.ABC
2016-08-08 16:49:24.062 宏定义的黑魔法[4125:213926] DEFGHIJKLMN.com.cn
2016-08-08 16:49:24.062 宏定义的黑魔法[4125:213926] AB
4.匹配字符串
NSString*string =@"www.ABCDEFGHIJKLMN.com.cn";
//判断字符串是否包含字符串,可以用来判断文件类型
if ([string hasPrefix:@"www."]) {//前
NSLog(@"字符串前面包含(www.)");
}
if ([string hasSuffix:@".com.cn"]) {//后
NSLog(@"字符串后面包含(.com.cn)");
}
if ([string rangeOfString:@"ABC"].location != NSNotFound) {
NSLog(@"这个字符串中有ABC");
}
//输出:
2016-08-08 16:55:31.014 宏定义的黑魔法[4295:221147] 字符串前面包含(www.)
2016-08-08 16:55:31.014 宏定义的黑魔法[4295:221147] 字符串后面包含(.com.cn)
2016-08-08 16:55:31.014 宏定义的黑魔法[4295:221147] 这个字符串中有ABC
5.分隔字符串
NSString *string = @"www.&A&B&C&D&E&F&G";
//将字符串以“&”为分割线,分割成数组
NSArray *separateElementsArray = [string componentsSeparatedByString:@"&"];
NSLog(@"\n%@",separateElementsArray);
//将数组元素拼接成字符串
NSString *string1 = [separateElementsArray componentsJoinedByString:@""];
NSLog(@"%@",string1);
//输出:
2016-08-08 17:04:29.913 宏定义的黑魔法[4470:229085]
(
"www.",
A,
B,
C,
D,
E,
F,
G
)
2016-08-08 17:04:29.913 宏定义的黑魔法[4470:229085] www.ABCDEFG
6
- (NSMutableAttributedString*)charactersString:(NSString*)str{
//纯数字,数组
NSMutableArray *stringValueArray = [NSMutableArray array];
NSMutableArray *characters = [NSMutableArray array];
NSMutableString *mutStr = [NSMutableString string];
//分离出字符串中的所有字符,并存储到数组characters中
for (int i = 0; i < str.length; i ++) {
NSString *subString = [str substringToIndex:i + 1];
subString = [subString substringFromIndex:i];
[characters addObject:subString];
}
//利用正则表达式,匹配数组中的每个元素,判断是否是数字,将数字拼接在可变字符串mutStr中
for (NSString *b in characters) {
NSString *regex = @"^[0-9]*$";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];// 谓词
BOOL isShu = [pre evaluateWithObject:b];// 对b进行谓词运算
if (isShu) {
[mutStr appendString:b];
} else {
if (mutStr.length>0) {
[stringValueArray addObject:mutStr];
mutStr = [NSMutableString string];
}
}
}
//此处已经将纯数字提取出来成功,
//NSLog(@"字符串(数字)数组:%@",stringValueArray);
//改变颜色
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:str];
[stringValueArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSRange range = [str rangeOfString:obj];
[attributeString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(range.location, range.length)];
}];
return attributeString;
}
- 用法实例:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *str = @"您的房源挂牌价格已经由250万调整为240万";
_elblTitle.attributedText = [self charactersString:str];
}