本文介绍了在Objective-C中使用NSString的大写或更改大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

c $ c> NSArray 包含索引2处的字符串'April'

我希望将其更改为'APRIL'

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

有这样简单吗?

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


推荐答案

这里ya go:

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

Btw:

april小写

APRIL UPPERCASE

四月五月 大写/ Word Caps

四月可能 句子上限(缺少方法;请参阅下面的解决方法)

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)

want被称为大写,而不是大写。 ;)

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

至于Sentence Caps,我们必须记住,通常Sentence是指整个字符串。如果你希望真正的句子使用第二个方法,下面,否则第一个:

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

这篇关于在Objective-C中使用NSString的大写或更改大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:49