0

I copied a number from native addressbook and it has possibly UTF-8 characters in the beginning and the end

NSString *phoneNumber = @"‭+488778689898‬";
NSLog(@"phonenumber.length --> %lu", (unsigned long)phoneNumber.length); //Prints 15
BOOL hasPlusPrefix = [phoneNumber hasPrefix:@"+"];
NSLog(@"hasPrefix -->%d", hasPlusPrefix); // Prints 0

When I debugged the number using the debugger it shows enter image description here

How do i remove the character in the front and at the end from my NSString.?

Thanks

Neelesh
  • 3,673
  • 8
  • 47
  • 78
  • Possible copy of: https://stackoverflow.com/questions/4663438/objective-c-find-numbers-in-string Try to remove letter from string and save only numbers: https://stackoverflow.com/a/27306065/1278744 – biloshkurskyi.ss Nov 23 '17 at 14:19
  • I am not understand this How do i remove the character in the front and at the end from my NSString.? – Anbu.Karthik Nov 23 '17 at 14:25
  • what have you tried so far to remove those characters? – holex Nov 23 '17 at 14:49
  • I tried to remove the additional characters by phoneNumber = [phoneNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; – Neelesh Nov 23 '17 at 15:31

1 Answers1

1

The codes you see in your string are for dealing with text direction. \U202d is the "LEFT-TO-RIGHT OVERRIDE" character and \U202c is the "POP DIRECTIONAL FORMATTING" character.

You can't remove them using whitespaceAndNewlineCharacterSet since they are not whitespace. But they are control codes and they can be removed using controlCharacterSet.

phoneNumber = [phoneNumber stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
rmaddy
  • 314,917
  • 42
  • 532
  • 579