ios - Split NSString from first whitespace -
i have name textfield in app, both firstname maybe middle , lastname written. want split these components first whitespace, space between firstname , middlename/lastname, can put model.
for example:
textfield text: john d. sowers
string 1: john
string 2: d. sowers.
i have tried using [[self componentsseparatedbycharactersinset:[nscharacterset whitespacecharacterset]] firstobject]; & [[self componentsseparatedbycharactersinset:[nscharacterset whitespacecharacterset]] lastobject]; these work if have name without middlename. since gets first , last object, , middlename ignored. how manage accomplish want?
/*fullnamestring nsstring*/ nsrange rangeofspace = [fullnamestring rangeofstring:@" "]; nsstring *first = rangeofspace.location == nsnotfound ? fullnamestring : [fullnamestring substringtoindex:rangeofspace.location]; nsstring *last = rangeofspace.location == nsnotfound ? nil :[fullnamestring substringfromindex:rangeofspace.location + 1]; ...the conditional assignment (rangeofspace.location == nsnotfound ? <<default value>> : <<real first/last name>>) protects against index out of bounds error.
Comments
Post a Comment