iOS 字符串截取、iOS 字符串替换、iOS 字符串分隔、iOS 字符串匹配、截取字符串、匹配字符串、分隔字符串

iOS之字符串截取、iOS 字符串替换、iOS字符串分隔、iOS之字符串匹配、截取字符串、匹配字符串、分隔字符串

1.iOS 字符串截取

//1.ios截取字符串
    
NSString *string =@"123456d890";
NSString *str1 = [string substringToIndex:5];//截取掉下标5之前的字符串
NSLog(@"截取的值为:%@",str1);
 NSString *str2 = [string substringFromIndex:3];//截取掉下标3之后的字符串
NSLog(@"截取的值为:%@",str2);

2.iOS 字符串替换

  //ios 替换某个字符

    NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@";" withString:@"."];

 //ios 替换某段字符

  NSString *idCard = [Str stringByReplacingCharactersInRange:NSMakeRange(2, 6) withString:@"XXXXXXXXX"];

3.iOS 字符串分隔

//3.分隔字符串
NSString *string =@"1234567";
    
NSArray *array = [string componentsSeparatedByString:@"4"]; //从字符A中分隔成2个元素的数组
NSLog(@"array:%@",array); //结果是123和567

5.iOS 字符串匹配

//2.匹配字符串
NSString *string =@"1234567";
NSRange range = [string rangeOfString:@"45"];//匹配得到的下标
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围内的字符串
NSLog(@"截取的值为:%@",string);

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页