NSString 遍历字符串的每一个字符并替换相关的字符
主要方法:获取子字符串:
substringWithRange:替换字符串:
stringByReplacingCharactersInRange:rangeNSString *string = @"this is my name";
for (int i = 0; i<[string length]; i++) {
//截取字符串中的每一个字符
NSString *s = [string substringWithRange:NSMakeRange(i, 1)];
NSLog(@"string is %@",s);
if ([s isEqualToString:@"m"]) {
NSRange range = NSMakeRange(i, 1);
//将字符串中的“m”转化为“w”
string = [string stringByReplacingCharactersInRange:range withString:@"w"];
}
}
NSLog(@"%@",string);
//截取字符串中的每一个字符
NSString *s = [string substringWithRange:NSMakeRange(i, 1)];
NSLog(@"string is %@",s);
if ([s isEqualToString:@"m"]) {
NSRange range = NSMakeRange(i, 1);
//将字符串中的“m”转化为“w”
string = [string stringByReplacingCharactersInRange:range withString:@"w"];
}
}
NSLog(@"%@",string);