ios - Extract one word from a two word string -


i have 2 word string in view controller containing user defined first , last name

nsstring *username = ([self hasattributewithname:kcontractorname] ? [self attributewithname:kcontractorname].value : [self.certificate.contractor.name uppercasestring]); 

when retrieving string in view controller want extract only first name.

i researched on using scanner , found helpful answer here: objective c: how extract part of string (e.g. start '#'), , im there.

the problem can seem extract second name variation on origial code. im scanning string space between first , second name, returns second name fine. need nudge on how set extract first name instead of second

    nsmutablearray *substrings = [nsmutablearray new];     nsscanner *scanner = [nsscanner scannerwithstring:username];     [scanner scanuptostring:@" " intostring:nil]; // scan characters before      while(![scanner isatend]) {         nsstring *name = nil;         [scanner scanstring:@" " intostring:nil]; // scan  character         if([scanner scanuptostring:@" " intostring:&name]) {             // if space followed , skipped             [substrings addobject:name];         }         [scanner scanuptostring:@" " intostring:nil]; // scan characters before next      } 

better use nsstring's componentsseparatedbystring method:

nsstring* firstname = [username componentsseparatedbystring:@" "][0]; 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -