objective c - capitalizedString doesn't capitalize correctly words starting with numbers? -
i'm using nsstring method [mystring capitalizedstring], capitalize words of string.
however capitalization doesn't work words starting numbers.
i.e. 2nd chance
becomes
2nd chance
even if n not first letter of word.
thanks
you have roll own solution problem. apple docs state may not specified behavior using function multi-word strings , strings special characters. here's pretty crude solution
nsstring *text = @"2nd place nothing"; // break string words separating on spaces. nsarray *words = [text componentsseparatedbystring:@" "]; // create new array hold capitalized versions. nsmutablearray *newwords = [[nsmutablearray alloc]init]; // want ignore words starting numbers. // class helps determine if string number. nsnumberformatter *num = [[nsnumberformatter alloc]init]; (nsstring *item in words) { nsstring *word = item; // if first letter of word not number (numberfromstring returns nil) if ([num numberfromstring:[item substringwithrange:nsmakerange(0, 1)]] == nil) { word = [item capitalizedstring]; // capitalize word. } // if number, don't change word (this implied). [newwords addobject:word]; // add word new list. } nslog(@"%@", [newwords description]);
Comments
Post a Comment