tags:

views:

85

answers:

2

Hi, I've created my own UITabBarController.

Additionally I've written a few lines of code to determine the current user.

E.g. if I am the current user do/display this, otherwise do/display this etc...

The format pattern is (firstname Lastname).

The Full name of the current user is in "displayName".

This is how I set the title of the tab depending on whether I am looking at 'my' tabs or someone else's tabs.

[activities setTitle:[viewingUser objectForKey:@"displayName"]];

I now want to extract only the firstname and display it like so:

"firstname's".

I do know of substringToIndex and substringWithRange but I just can't seem to work it out myself. I reckon I just need to find the first and extract the part it togehter with that ['s]. Can anybody please point me in the right direction?

Cheers

A: 

Take a look at the NSScanner documentation and associated sample code. There are simpler ways to do it if that is your only dataset, however, the moment you start getting into even semi-complex sequences, you'll need other, more powerful solutions. This is why I'm recommending NSScanner off the top.

jer
A: 

If first name and last name are separated by a space then simply execute the following statement which returns an NSArray which in your will contain the first and last names.

[displayName componentsSeparatedByString:@" "]
ennuikiller
Yes, thanks for that.I got it working like so:[code]NSCharacterSet *delimiter = [NSCharacterSet characterSetWithCharactersInString:@" "];NSArray *splitString = [[viewingUser objectForKey:@"displayName"] componentsSeparatedByCharactersInSet:delimiters];NSString *xString = [splitString objectAtIndex:0]; //This contains the firstname //NSString *yString = [splitString objectAtIndex:1]; //This contains the lastname[/code]Can be called like so:[activities setTitle:[NSString stringWithFormat:@"%@ %@",xString, @"'s View"]];