简体   繁体   中英

objective-c string manipulation

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

NSInteger hyphenStart = [theString rangeOfString:@" - "].location;
if(hyphenStart == NSNotFound)
    return nil;
return [theString substringToIndex:hyphenStart];

阅读NSString类文档。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM