美文网首页
iOS字符串删除控制符

iOS字符串删除控制符

作者: iOS_zy | 来源:发表于2020-08-20 11:11 被阅读0次

    今天遇到这么个问题
    服务端返回一段jsonString, 需要转成数组使用
    但是经过转换后发现是nil
    转换方法:
    JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
    报错:
    Unescaped control character around character XX
    解决办法:

    +(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;
    }
    

    相关文章

      网友评论

          本文标题:iOS字符串删除控制符

          本文链接:https://www.haomeiwen.com/subject/izdsjktx.html