3
surnameField.text = "Fal à èà ò l'opo";    

// remove space and apostrophe
NSString *surnarmeInput = [[surnameField.text stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"'" withString:@""];

I would remove also accents.

Result "Falaeaolopo"

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Maurizio
  • 89
  • 2
  • 15

3 Answers3

11
// convert to a data object, using a lossy conversion to ASCII
NSData *asciiEncoded = [yourOriginalString dataUsingEncoding:NSASCIIStringEncoding
                         allowLossyConversion:YES];

// take the data object and recreate a string using the lossy conversion
NSString *other = [[NSString alloc] initWithData:asciiEncoded
                                        encoding:NSASCIIStringEncoding];
// relinquish ownership
[other autorelease];

which will remove all the accents..To remove all spaces

NSString *yourFinalString = [other stringByReplacingOccurrencesOfString:@" " withString:@""];

First part of removing accent, code copied from dreamlax's answer in this thread..

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • Careful, this remove not only accents, remove other characters like the Spanish letter `ñ` for example. – jherran Jan 21 '15 at 12:33
1

Using dataUsingEncoding:allowLossyConversion: doesn't seem to work for some characters - Đ and đ for example. I use a specific check for these, followed by the ascii encoding.

Rev. Andy
  • 81
  • 1
  • 5
0

Try the dataUsingEncoding:allowLossyConversion: method in NSString.

Marcelo Alves
  • 1,856
  • 11
  • 12