iOS 截取字符串,分割字符串

//    1.截取字符串
    NSString *string = @"01234567890123456789";
    string = [string substringToIndex:7];//截取掉下标7之前的字符串
    NSLog(@"string:%@",string);//0123456
    
    string = @"01234567890123456789";
    string = [string substringFromIndex:2];//截取掉下标2之后的字符串
    NSLog(@"string:%@",string);//234567890123456789

//    2.匹配字符串
    string = @"01234567890123456789";
    NSRange range = [string rangeOfString:@"90"];//匹配得到的下标
    NSLog(@"rang:%@",NSStringFromRange(range));//rang:{9, 2}
    
    string = @"01234567890123456789";
    string = [string substringWithRange:range];//截取范围类的字符串
    NSLog(@"string:%@",string);//90

//    3.分隔字符串
    string = @"01234567890123456789";
    NSArray *array = [string componentsSeparatedByString:@"6"]; //从字符6中分隔成3个元素的数组
    NSLog(@"array:%@",array); //array:(012345,789012345,789)
    
//    4.字符串替换
    string = @"01234567890123456789";
    string = [string stringByReplacingOccurrencesOfString:@"6" withString:@"*"];//替换6为*
    NSLog(@"string:%@",string);//012345*789012345*789

https://www.jianshu.com/p/36e29256041d

你可能感兴趣的:(iOS 截取字符串,分割字符串)