How to remove hexadecimal characters from NSString

前端 未结 3 1676
时光取名叫无心
时光取名叫无心 2021-01-22 14:49

I am facing one issue related some hexa value in string, i need to remove hexadecimal characters from NSString.

The problem is when i print object it prints as \"BLANK

3条回答
  •  走了就别回头了
    2021-01-22 15:49

    I'm not sure exactly what you are looking for, but if you want to remove all the control characters then

    string = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet controlCharacterSet]] componentsJoinedByString:@""]
    

    If you need to be faster and are sure the control characters are only at the beginning and ending of a string then

    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
    

    NOTE: Removing all control characters will remove all new lines (\n)!


    From NSCharacterSet Class Reference:

    These characters are specifically the Unicode values U+0000 to U+001F and U+007F to U+009F.

    The value you are having a problem with is \x06 which is U+0006.


    If you want to remove just \x06, then you can always create a characters set just for it.

    NSCharacterSet *hex6 = [NSCharacterSet characterSetWithCharactersInString:@"\x06"];
    string = [[string componentsSeparatedByCharactersInSet:hex6] componentsJoinedByString:@""]
    

提交回复
热议问题