嗨,我有一个像这样的字符串
NSString *str=@"1,2,3,4";
我必须删除动态中的任何字符。例如,@"3,"
。
答案 0 :(得分:80)
NSString *str=@"1,2,3,4";
[str stringByReplacingOccurrencesOfString:@"3," withString:@""];
这将删除str
中所有出现的@“3”。
如果您只想删除第一次出现的@“3,”:
NSString* str = @"1,2,3,4";
NSRange replaceRange = [str rangeOfString:@"3,"];
if (replaceRange.location != NSNotFound){
NSString* result = [str stringByReplacingCharactersInRange:replaceRange withString:@""];
}
希望这有帮助。
答案 1 :(得分:6)
NSString *str=@"1,2,3,4";
int numberToRemove = 4;
str = [str stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%d",numberToRemove] withString:@""];
str = [str stringByReplacingOccurrencesOfString:@",," withString:@","];
这会有所帮助。
答案 2 :(得分:0)
这对我有用,如果有人会帮助!
NSString *newStr = @"";
newStr = [str stringByReplacingOccurrencesOfString:@"3," withString:@""];