【问题标题】:How to replace the \u00e2\u0080\u0099 this string into ' in iOS如何在iOS中将\u00e2\u0080\u0099这个字符串替换为'
【发布时间】:2014-02-07 02:28:14
【问题描述】:

我从服务器获得了一个值,并将其存储在 NSString 对象中。在该字符串中,“'”被替换为 \u00e2\u0080\u0099。我尝试使用replaceOccurencesOfString 来替换它。但它显示一个错误。在此我显示了我的代码和错误。

 [mutstr_Description replaceOccurrencesOfString:@"\u00e2\u0080\u0099" withString:@"'" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutstr_Description length])];

帮助解决这个问题。

【问题讨论】:

    标签: ios objective-c xcode nsstring nsstringencoding


    【解决方案1】:

    假设你的字符串实际上有文字字符\u0等,你需要这样做:

    [mutstr_Description replaceOccurrencesOfString:@"\\u00e2\\u0080\\u0099" withString:@"'" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutstr_Description length])];
    

    您需要转义反斜杠。

    更新——也许你的字符串实际上有那些控制字符。试试这个:

    NSString *controlChars = [NSString stringWithFormat:@"%C%C%C", 0xe2, 0x80, 0x99];
    [mutstr_Description replaceOccurrencesOfString:controlChars withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [mutstr_Description length])];
    

    【讨论】:

    • 我试过你说的。现在错误没有显示。但内容不会被替换。 [mutstr_Description replaceOccurrencesOfString:@"\\u002\\u0080\\u0099" withString:@"'" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutstr_Description length])];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2019-05-11
    • 2021-06-01
    相关资源
    最近更新 更多