NSString - how to go from "ÁlgeBra" to "Algebra"

compare, iphone, nsstring

Solution

`NSString` has a method called `capitalizedString`:

Return Value

A string with the first character from each word in the receiver changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values.

NSString *str = @"AlgeBra";
NSString *other = [str capitalizedString];

NSLog (@"Old: %@, New: %@", str, other);

Edit:

Just saw that you would like to remove accents as well. You can go through a series of steps:

// original string
NSString *str = @"ÁlgeBra";

// convert to a data object, using a lossy conversion to ASCII
NSData *asciiEncoded = [str 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];

// create final capitalized string
NSString *final = [other capitalizedString];

The documentation for `dataUsingEncoding:allowLossyConversion:` explicitly says that the letter ‘Á’ will convert to ‘A’ when converting to ASCII.

Problem

Does anyone knows hoe to get a NSString like "ÁlgeBra" to "Algebra", without the accent, and capitalize only the first letter? Thanks, RL

Original source