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

发布于 2024-10-31 08:29:01 字数 259 浏览 1 评论 0原文

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

NSArray 在索引 2 处包含字符串 'April'
我希望将其更改为'APRIL'

有这样简单的事情吗?

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

I was wondering how to capitalize a string found in an object in an NSMutableArray.

An NSArray contains the string 'April' at index 2.
I want this to be changed to 'APRIL'.

Is there something simple like this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

留一抹残留的笑 2024-11-07 08:29:01

给你:

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

顺便说一句:
“april”小写[NSString lowercaseString]
“APRIL”大写[NSString uppercaseString]
“April May”大写/单词大写[NSString CapitalizedString]
“April may”句子大写(方法缺失;请参阅下面的解决方法)

因此,您想要的称为“大写”,而不是“大写”。 ;)

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

@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

Here ya go:

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

Btw:
"april" is lowercase[NSString lowercaseString]
"APRIL" is UPPERCASE[NSString uppercaseString]
"April May" is Capitalized/Word Caps[NSString capitalizedString]
"April may" is Sentence caps(method missing; see workaround below)

Hence what you want is called "uppercase", not "capitalized". ;)

As for "Sentence Caps" one has to keep in mind that usually "Sentence" means "entire string". If you wish for real sentences use the second method, below, otherwise the first:

@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
余生再见 2024-11-07 08:29:01
viewNoteDateMonth.text  = [[displayDate objectAtIndex:2] uppercaseString];

文档:

" 还可以使用 lowercaseStringcapitalizedString

焚却相思 2024-11-07 08:29:01

如果有人在 swift 中需要上述内容:

SWIFT 3.0 及更高版本:

这将使您的字符串大写,使第一个字母大写:

viewNoteDateMonth.text  = yourString.capitalized

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

viewNoteDateMonth.text  = yourString.uppercased()

In case anyone needed the above in swift :

SWIFT 3.0 and above :

this will capitalize your string, make the first letter capital :

viewNoteDateMonth.text  = yourString.capitalized

this will uppercase your string, make all the string upper case :

viewNoteDateMonth.text  = yourString.uppercased()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文