NSStringメソッド[myString capitalizedString]を使用して、文字列のすべての単語を大文字にします。capitalizedStringは数字で始まる単語を正しく大文字に変換しませんか?
ただし、数字で始まる単語では大文字の使用がうまく機能しません。
i.e. 2nd chance
nは単語の最初の文字ではない場合であっても
2Nd Chance
なります。
ありがとう
NSStringメソッド[myString capitalizedString]を使用して、文字列のすべての単語を大文字にします。capitalizedStringは数字で始まる単語を正しく大文字に変換しませんか?
ただし、数字で始まる単語では大文字の使用がうまく機能しません。
i.e. 2nd chance
nは単語の最初の文字ではない場合であっても
2Nd Chance
なります。
ありがとう
あなたはこの問題に独自のソリューションを展開する必要があります。 Apple docsは、マルチワード文字列や特殊文字を含む文字列に対して、その関数を使用して指定された動作を取得できない可能性があると述べています。ここではかなり粗溶液は、これが役立つかもしれない
NSString *text = @"2nd place is nothing";
// break the string into words by separating on spaces.
NSArray *words = [text componentsSeparatedByString:@" "];
// create a new array to hold the capitalized versions.
NSMutableArray *newWords = [[NSMutableArray alloc]init];
// we want to ignore words starting with numbers.
// This class helps us to determine if a string is a number.
NSNumberFormatter *num = [[NSNumberFormatter alloc]init];
for (NSString *item in words) {
NSString *word = item;
// if the first letter of the word is not a number (numberFromString returns nil)
if ([num numberFromString:[item substringWithRange:NSMakeRange(0, 1)]] == nil) {
word = [item capitalizedString]; // capitalize that word.
}
// if it is a number, don't change the word (this is implied).
[newWords addObject:word]; // add the word to the new list.
}
NSLog(@"%@", [newWords description]);
良い解決策。私は '[[newWords valueForKey:@" description "] componentsJoinedByString:@" "];'のために '[newWords description];'を変更しましたが、前者は括弧と改行文字を含む文字列を返します。 –
私の問題を解決しました – Hassy
残念ながら、これはcapitalizedStringの一般的な動作のようです。
おそらくあまりうまくいかない回避策/ハックは、変換前に各数値を文字列に置き換えてから、それを後で元に戻すことです。だから、
、 "第二のチャンス" - > "xyzndチャンス" - > "Xyzndチャンス" - > "第二のチャンス"
確かにそれほど素晴らしいハックではありません、他の解決策はありますか? – aneuryzm
です:http://www.pittle.org/weblog/how-to-capitalize-a-nsstring-instance-while-keeping-roman-numerals-all-capitalized_536 .html –