在 Objective-C 中大写或更改 NSString 的大小写

我想知道如何大写字符串中找到的对象在 NSMutableArray

NSArray包含索引2处的字符串 'April'
我想把这个改成 'APRIL'

有这么简单的东西吗?

viewNoteDateMonth.text  = [[displayDate objectAtIndex:2] capitalized];
112739 次浏览

给你:

viewNoteDateMonth.text  = [[displayDate objectAtIndex:2] uppercaseString];

顺便说一句:
"april"lowercase something < a href = “ http://developer.apple.com/library/mac/document/Cocoa/Reference/Foundation/Clases/NSString _ Class/Reference/NSString.html #//apple _ ref/occ/instm/NSString/lowercaseString”rel = “ noReferrer”> [ NSString lowercaseString ]
"APRIL"UPPERCASE something < a href = “ http://developer.apple.com/library/mac/document/Cocoa/Reference/Foundation/Clases/NSString _ Class/Reference/NSString.html #//apple _ ref/occ/instm/NSString/uppercaseString”rel = “ noReferrer”> [ NSString uppercaseString ]
"April May"Capitalized/Word Caps something < a href = “ http://developer.apple.com/library/mac/document/Cocoa/Reference/Foundation/Class/NSString _ Class/Reference/NSString.html #//apple _ ref/occ/instm/NSString/capital alizedString”rel = “ noReferrer”> [ NSString capital alizedString ]
"April may"Sentence caps something < em > (缺少方法; 请参阅下面的解决方案)

因此,您需要的是“ 大写”,而不是“ 资本化”。 ;)

至于“句子大写”,人们必须记住,通常“句子”意味着“整个字符串”。如果你想要真正的句子,可以使用下面的第二种方法,否则就使用第一种方法:

@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

万一有人需要以上这些:

SWIFT 3.0及以上版本:

字符串大写,第一个字母大写:

viewNoteDateMonth.text  = yourString.capitalized

这将大写你的字符串,使所有的字符串大写:

viewNoteDateMonth.text  = yourString.uppercased()