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

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -