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"

3 Answers 3

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..

1
  • Careful, this remove not only accents, remove other characters like the Spanish letter ñ for example.
    – jherran
    Jan 21, 2015 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.

0

Try the dataUsingEncoding:allowLossyConversion: method in NSString.

2
  • i think is not applicable method in NSString
    – Maurizio
    Nov 1, 2011 at 10:56
  • Just convert the NSData to a String with a initWithData:encoding: (NSString). Nov 1, 2011 at 10:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.