views:

47

answers:

2

I have a string "AA - The Title of the Person"

How can I get the part before the '-'?

The output should be "AA"

Also if the input doesn't have a '-' then output should be nil

+5  A: 
NSInteger hyphenStart = [theString rangeOfString:@" - "].location;
if(hyphenStart == NSNotFound)
    return nil;
return [theString substringToIndex:hyphenStart];
Noah Witherspoon
will this give me "AA " or just "AA"?
Yolanda
it will give you "AA"
Jesse Naugher
ok thx. How can I trim a string in obj-c?
Yolanda
The NSString class docs are quite good, and the class has a lot of functionality.
Jesse Naugher
A: 

Have a read of the NSString class documentation.

Am