在Objective-C中大写或改变NSString的大小写
我想知道如何大写在NSMutableArray
中的对象中find的string。
一个NSArray
在索引2处包含string'April'
。
我想把这个改成'APRIL'
。
有这样简单的东西吗?
viewNoteDateMonth.text = [[displayDate objectAtIndex:2] capitalized];
这里你去:
viewNoteDateMonth.text = [[displayDate objectAtIndex:2] uppercaseString];
BTW:
"april"
是lowercase
➔ [NSString lowercaseString]
"APRIL"
是UPPERCASE
[NSString大写string]
"April May"
是Capitalized/Word Caps
➔ [NSString大写字母]
"April may"
是Sentence caps
➔ (缺less方法;请参阅下面的解决方法)
因此,你想要的是所谓的“ 大写 ”,而不是“ 大写 ”。 ;)
至于“句子帽”,必须记住,通常“句子”是指“整个string”。 如果你想要真正的句子,使用下面的第二种方法,否则第一种:
@interface NSString () - (NSString *)sentenceCapitalizedString; // sentence == entire string - (NSString *)realSentenceCapitalizedString; // sentence == real sentences @end @implementation NSString - (NSString *)sentenceCapitalizedString { if (![self length]) { return [NSString string]; } NSString *uppercase = [[self substringToIndex:1] uppercaseString]; NSString *lowercase = [[self substringFromIndex:1] lowercaseString]; return [uppercase stringByAppendingString:lowercase]; } - (NSString *)realSentenceCapitalizedString { __block NSMutableString *mutableSelf = [NSMutableString stringWithString:self]; [self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationBySentences usingBlock:^(NSString *sentence, NSRange sentenceRange, NSRange enclosingRange, BOOL *stop) { [mutableSelf replaceCharactersInRange:sentenceRange withString:[sentence sentenceCapitalizedString]]; }]; return [NSString stringWithString:mutableSelf]; // or just return mutableSelf. } @end
viewNoteDateMonth.text = [[displayDate objectAtIndex:2] uppercaseString];
文档: http : //developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/uppercaseString
你也可以使用lowercaseString和capitalizedString