字符串的常用处理,删除、替换。记录一下,方便查找


- (void)viewDidLoad {  
    [super viewDidLoad];  
      
    NSString *str1 = @"<hello,wo  r d!>";  
    //删除字符串两端的尖括号  
    NSMutableString *mString = [NSMutableString stringWithString:str1];  
    //第一个参数是要删除的字符的索引,第二个是从此位开始要删除的位数  
    [mString deleteCharactersInRange:NSMakeRange(0, 1)];  
    [mString deleteCharactersInRange:NSMakeRange(mString.length-1, 1)];  
    NSLog(@"mString:%@",mString);  
      
    //删除字符串中的空格  
    NSString *str2 = [mString stringByReplacingOccurrencesOfString:@" " withString:@""];  
    NSLog(@"str2:%@",str2);  
      
    //同样的可以替换字符  
    NSString *str3 = [str2 stringByReplacingOccurrencesOfString:@"," withString:@"好"];  
    NSLog(@"str3:%@",str3);  
      
    //替换某一位置的字符  
    NSString *str4 = [str3 stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"哈哈"];  
    NSLog(@"str4:%@",str4);  
}