字符串截取、替换、修饰

1 /* To avoid breaking up character sequences such as Emoji, you can do:
2     [str substringFromIndex:[str rangeOfComposedCharacterSequenceAtIndex:index].location]
3     [str substringToIndex:NSMaxRange([str rangeOfComposedCharacterSequenceAtIndex:index])]
4     [str substringWithRange:[str rangeOfComposedCharacterSequencesForRange:range]
5 */
6 - (NSString *)substringFromIndex:(NSUInteger)from;
7 - (NSString *)substringToIndex:(NSUInteger)to;
8 - (NSString *)substringWithRange:(NSRange)range;                // Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up character sequences

1、通过下标获取字符串

OC

1 NSString * str = @"12345abcde";
2 NSString *logStr = [str substringFromIndex:6]; //从字符串下标为 6 的开始,截止字符串末尾
1 NSString * str = @"12345abcde";
2 NSString *logStr = [str substringToIndex:3]; //从字符串开始,截止字符串下标为 3 
 1 /**
 2 typedef struct _NSRange {
 3     NSUInteger location;
 4     NSUInteger length;
 5 } NSRange;
 6 
 7 typedef NSRange *NSRangePointer;
 8 
 9 NS_INLINE NSRange NSMakeRange(NSUInteger loc, NSUInteger len) {
10     NSRange r;
11     r.location = loc;
12     r.length = len;
13     return r;
14 }
15 */
16 
17 NSString * str = @"12345abcde";
18 NSString *logStr = [str substringWithRange:NSMakeRange(2, 4)]; //从下标为2开始,截取长度为4的字符串

JS

var str = "这是一个很长的字符串"     
console.log(str.substring(0, 3)) //输出 这是一

 

2、匹配查找字符串

1  NSString * str = @"12345abcde";
2  NSRange *logRange = [str rangeOfString:@"ab"]; //返回匹配到字符串的起始下标及长度

3、分割字符串

OC

1 NSString * str = @"12345abcde";
2 NSArray *logArr = [str componentsSeparatedByString:@"a"]; //以a为分隔符,返回分割后的字符串数组
3 //数组元素拼接为字符串
4 NSString *toStr = [logArr componentsJoinedByString:@","];
5 //toStr  12345,bcde

Swift

let urlScheme = "baidumap://map/walknavi?origin=40.057406655722,116.2964407172&destination=39.91441,116.40405" 
let strArr = urlScheme.components(separatedBy: "://")
print(strArr) //输出:[baidumap,map/walknavi?origin=40.057406655722,116.2964407172&destination=39.91441,116.40405]

 

4、字符串替换

OC

 NSString *str = @"18888888888";
 NSString *logString = [str stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"]; //隐藏手机号中间四位数
 NSLog(@"%@",logString);//输出结果为188****8888
NSString *content = @"1&2&3&4&5&6";
NSString *replaceStr = [content stringByReplacingOccurrencesOfString:@"&" withString:@","];//用 , 替换 &
DebugLog(@"结果:%@",replaceStr);//输出结果 1,2,3,4,5,6

Swift

let content = "1&2&3&4&5&6" 
let replaceStr = content.replacingOccurrences(of:"&", with:",")
print(replaceStr) // 输出:1,2,3,4,5,6

5、字符串大小写转换

/**
/* The following three return the canonical (non-localized) mappings. They are suitable for programming operations that require stable results not depending on the user's locale preference.  For locale-aware case mapping for strings presented to users, use the "localized" methods below.
*/
@property (readonly, copy) NSString *uppercaseString;
@property (readonly, copy) NSString *lowercaseString;
@property (readonly, copy) NSString *capitalizedString;
*/
NSString *str1 = @"abcdef123";
NSString *str2 = @"QWER456";
//字符串的英文字符由小写转换成大写
NSString *bigStr = [str1 uppercaseString];
NSLog(@"%@",bigStr); //输出结果为    ABCDEF123
//字符串的英文字符由大写转换为小写
NSString *smallStr = [str2 lowercaseString];
NSLog(@"%@",smallStr);//输出结果为  qwer456
//字符串的首字母改为大写
NSString *FirstletterbiggerStr = [str1 capitalizedString];
NSLog(@"%@",FirstletterbiggerStr);//输出结果为Abcdef123

6、字符串部分上色、修改样式

1 NSString *Str = @"我自愿加入原创者联盟";
2 NSMutableAttributedString *MutableStr = [[NSMutableAttributedString alloc] initWithString:Str];
3 [MutableStr addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 5)];
4 //同样如果是改变字符串中某段字体的大小可以这样写:
5 [MutableStr addAttribute:NSFontAttributeName value:[UIFont SystemFontOfSize:28] range:NSMakeRange(5, 5)];

 

posted @ 2020-04-27 17:08  奶瓶瓶🍼  阅读(565)  评论(0编辑  收藏  举报