Replace unicode value in string

雨燕双飞 提交于 2019-12-10 12:17:48

问题


I have a string @"\EOP". I want to dislpay this to user. But when i display this string in textfield, It shows only OP. I tried to print that in console while debugging and it shows ¿OP

So \E is unicode value and that's why it's having some issue of encoding. I can fix this issue by:

NSString *str=[str stringByReplacingOccurrencesOfString:@"\E" withString:@"\\E"];

With this it will display perfect string @"\EOP".

Here my issue is that there can be many more characters same like \E for example \u. How can I implement one fix for all these kind of characters?


回答1:


\E in the string @"\EOP" is the character with the ASCII-code (or Unicode) 27, which is a control character.

I don't know of a built-in method to escape all control characters in a string. The following code uses NSScanner to locate the control characters, and replaces them using a lookup-table. The control characters are replaced by "Character Escape Codes" such as "\r" or "\n" if possible, otherwise by "\x" followed by the hex-code.

NSString *str = @"\EOP";

NSCharacterSet *controls = [NSCharacterSet controlCharacterSet];
static char *replacements[] = {
    "0", NULL, NULL, NULL, NULL, NULL, NULL, "\\a",
    "\\b", "\\t", "\\n", "\\v", "\\f", "\\r", NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, "\\e"};

NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner setCharactersToBeSkipped:nil];
NSMutableString *result = [NSMutableString string];

while (![scanner isAtEnd]) {
    NSString *tmp;
    // Copy all non-control characters verbatim:
    if ([scanner scanUpToCharactersFromSet:controls intoString:&tmp]) {
        [result appendString:tmp];
    }
    if ([scanner isAtEnd])
        break;
    // Escape all control characters:
    if ([scanner scanCharactersFromSet:controls intoString:&tmp]) {
        for (int i = 0; i < [tmp length]; i++) {
            unichar c = [tmp characterAtIndex:i];
            char *r;
            if (c < sizeof(replacements)/sizeof(replacements[0])
                && (r = replacements[c]) != NULL) {
                // Replace by well-known character escape code:
                [result appendString:@(r)];
            } else {
                // Replace by \x<hexcode>:
                [result appendFormat:@"\\x%02x", c];
            }
        }
    }

}

NSLog(@"%@", result);



回答2:


You can always replace \ with \\. These are called Escape Sequences.

Sample Code :

NSString *str = @"\EOP";
NSString *myNewStr = [str stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
NSLog(@"myNewStr :: %@",myNewStr);



回答3:


If you want a backslash (\) to appear in a string literal, you need to escape it in the string literal i.e.

NSString* foo = @"\\EOP";

The above will give you the Unicode sequence 5C 45 4F 50 which is what you want.



来源:https://stackoverflow.com/questions/19564950/replace-unicode-value-in-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!