Curnelious
Curnelious

Reputation: 1

get NSString first word only

I am having this NSString, that gives me the user language .

NSString * language= @"English (America) " ;

I need to get from it ONLY the first word- which is the language . I know how to find space, but could not find a simple way to get only the first word.

ThAnks.

Upvotes: 11

Views: 9077

Answers (3)

aahsanali
aahsanali

Reputation: 3547

Here is my answer:

  NSString * language= @"English (America) " ;

  NSArray *comps = [language componentsSeparatedByString:@" "];

  NSString * eng = [comps objectAtIndex:0];

Upvotes: 2

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

NSUInteger location = [language rangeOfString:@" ("].location;
NSString *result = location == NSNotFound ? language : [language substringToIndex:location];

Upvotes: 6

Fogmeister
Fogmeister

Reputation: 77641

NSString *firstWord = [[language componentsSeparatedByString:@" "] objectAtIndex:0];

Upvotes: 48

Related Questions