我正在从 NSString 动态追加和删除子字符串-
在特定的操作中,我正在附加使用(我在添加新字符串时使用逗号分隔符)-
self.selectedString = [self.selectedString length] < 1 ? newSelectedString
: [self.selectedString stringByAppendingFormat:@",%@",newSelectedString];
删除 - 现在这个逗号在删除字符串时给我带来了问题。目前我正在为此使用解决方案 -
self.selectedString = [self.selectedString stringByReplacingOccurrencesOfString:newSelectedString
withString:@""];
NSRange rangeSingleComma = [self.selectedString rangeOfString:@","];
NSRange rangeDoubleComma = [self.selectedString rangeOfString:@",,"];
if (rangeSingleComma.location == [self.selectedString length] - 1) {
self.selectedString = [self.selectedString substringToIndex:[self.selectedString length] - 1];
}
if (rangeSingleComma.location == 0) {
self.selectedString = [self.selectedString substringFromIndex:1];
}
if (rangeDoubleComma.location != NSNotFound) {
self.selectedString = [self.selectedString stringByReplacingOccurrencesOfString:@",,"
withString:@","];
}
但这是一种非常肮脏的方法,任何人都可以为此提出一个好的方法。