- 问题:在json字符串转换为对象的时候报错,字面意思为 数据中出现了被控制- 的符号
- 解决思路:遍历字符串,找到对应的控制符,删除之
- 代码
+(NSString *)deleteControlCharacter:(NSString *)originalString{
NSMutableString *mstring = [NSMutableString stringWithString:originalString];
NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet];
NSRange range = [originalString rangeOfCharacterFromSet:controlChars];
while (range.location != NSNotFound){//包含
[mstring deleteCharactersInRange:range];//删除
range = [mstring rangeOfCharacterFromSet:controlChars];//递归
}
return mstring;
}
完。
=========2020-08-31更新============
应网友要求,添加swift版本
func deleteControlCharacter(oriString:String)-> String {
let ocStr = oriString as NSString
let ocMStr = NSMutableString.init(string: ocStr)
let set = CharacterSet.controlCharacters
var range = ocMStr.rangeOfCharacter(from: set)
while range.location != NSNotFound {
ocMStr.deleteCharacters(in: range)
range = ocMStr.rangeOfCharacter(from: set)
}
let resultStr = ocMStr as String
return resultStr
}