Separate a string into different names then reorder each - iPhone
Basically I have a string with the value:
Bruce Lee, Jackie Chan, Micky Rourke, Vinnie Jones,
what I'd like to do is convert the above into something like:
Lee, B., Chan, J., Rourke, M., Jones,开发者_开发知识库 V.
I'm sure this can be done but i'm totally new to string manipulation and iPhone development. NSScanner seems useful but after reading the documentation I'm still unsure how to do this.
This is an impossible problem to solve reliably. Think of names like "van der Something" or those with two surnames. What about multiple first names? Those who go by their full (given, middle, family) names? What of those with extremely long multi-word names? There are many nationalities and the format commonly referred to as "Christian names" in Western culture is by no means the majority or any kind of standard.
In general you can separate "tokens" (words) by using -[NSString componentsSeparatedByString:@" "] (passing a space as the separator), which hands you an array of substrings, but you'll likely have to provide additional UI to allow the user to correct any names that don't follow the traditional Westerner name style (Firstname Lastname).
As for reordering you can sort arrays easily by using NSMutableArray's many -sort... methods or NSArray's many -sortedArrays... methods. The documentation is your friend.
精彩评论