Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS How to remove Zero Width Space [ E2 80 8B ] from NSString

With copy/paste, one of my clients put in a textfield of my IOS app a text containing Zero Width Space [ E2 80 8B ] and I want to remove them.

here's an example of text : basse ​température ​avec ​dégivrage ​électrique

what I tried :

NSString* zarb = [NSString stringWithFormat:@"%c%c%c",0xE2,0x80,0x8B];
NSString*resu=[ch stringByReplacingOccurrencesOfString:zarb withString:@""];
// does not work

if ([ch rangeOfString:zarb].location != NSNotFound) {
      // does not work
}

The hexa sequence IS in the string but I cannot remove it. Someone has already got this problem ?

like image 397
P.KOD Avatar asked May 13 '13 19:05

P.KOD


People also ask

How do you remove zero width space from a string?

To remove zero-width space characters from a JavaScript string, we can use the JavaScript string replace method that matches all zero-width characters and replace them with empty strings. Zero-width characters in Unicode includes: U+200B zero width space.

How to remove zero width space in Python?

Use the str. replace() method to remove zero width space characters from a string, e.g. result = my_str. replace('\u200c', '') .


1 Answers

The "zero width space" is the Unicode character \U200B. The E2 80 8B is the UTF-8 encoding.

Try this:

NSString* zarb = @"\u200B";
NSString* resu = [ch stringByReplacingOccurrencesOfString:zarb withString:@""];

BTW - your attempt to do:

NSString* zarb = [NSString stringWithFormat:@"%c%c%c",0xE2,0x80,0x8B];

results in an invalid string because there are no Unicode characters for 80 and 8B.

like image 75
rmaddy Avatar answered Sep 21 '22 15:09

rmaddy