Maurizio
Maurizio

Reputation: 89

remove all accents by string

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

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

I would remove also accents.

Result "Falaeaolopo"

Upvotes: 3

Views: 3029

Answers (3)

Rev. Andy
Rev. Andy

Reputation: 81

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.

Upvotes: 1

Krishnabhadra
Krishnabhadra

Reputation: 34275

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

Upvotes: 11

Marcelo Alves
Marcelo Alves

Reputation: 1846

Try the dataUsingEncoding:allowLossyConversion: method in NSString.

Upvotes: 0

Related Questions