大写的第一个字母在NSString中

我怎样才能大写NSString的fisrt字母,并删除任何口音?

例如, ÀlterAlteralter应该变成 Alter

但是, :lter应该保持不变 ,因为第一个字符不是字母。

既然你想删除变音标记,你可以使用这个方法结合常见的string操作方法,如下所示:

 /* create a locale where diacritic marks are not considered important, eg US English */ NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"] autorelease]; NSString *input = @"Àlter"; /* get first char */ NSString *firstChar = [input substringToIndex:1]; /* remove any diacritic mark */ NSString *folded = [firstChar stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:locale]; /* create the new string */ NSString *result = [[folded uppercaseString] stringByAppendingString:[input substringFromIndex:1]]; 

请不要使用这种方法。 因为一个字母在不同的语言中可能有不同的数量。 你可以检查梦想的答案。 但我相信你会从我的答案中学到一些东西。 快乐编码:)

 NSString *capitalisedSentence = nil; //Does the string live in memory and does it have at least one letter? if (yourString && yourString.length > 0) { // Yes, it does. capitalisedSentence = [yourString stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[yourString substringToIndex:1] capitalizedString]]; } else { // No, it doesn't. } 

我为什么要关心字母的数量?

如果你尝试访问(例如NSMakeRangesubstringToIndex等)空string中的第一个字符,如@"" ,那么你的应用程序将崩溃。 为了避免这种情况,您必须在处理之前validation它是否存在。

如果我的string是零呢?

尼尔先生: 我没有。 我可以消化你发给我的任何东西。 我不会让你的应用程序本身崩溃。 ;)

一个人吞下假炸药的动画,它在肚子里冒出来,然后用卡通的方式从他的嘴里冒出没有受伤的烟。

nil将观察您发送给它的任何方法调用。

所以它会消化你尝试的任何东西, nil是你的朋友。

你可以使用NSString的:

- (NSString *)capitalizedString

或(iOS 6.0及以上):

- (NSString *)capitalizedStringWithLocale:(NSLocale *)locale

要删除我认为可以用来完成这个步骤的一系列步骤。 希望你可以按照没有概率! 🙂

  • 使用decomposedStringWithCanonicalMapping分解任何重音符号(重要的是要确保重音字符不只是不必要的删除)
  • 使用characterAtIndex:提取第一个字母(索引0),使用upperCaseString将其转换为stringByReplacingCharactersInRange字母并使用stringByReplacingCharactersInRange将第一个字母replace回原始string。
  • 在这个步骤中,在把它变成大写字母之前,你可以检查第一个字母是否是你不想replace的字符之一,例如“:”或“;”,如果是,就不要跟着的程序。
  • 做一个[theString stringByReplacingOccurrencesOfString:@" ”withString:@“”]`types的调用,以消除任何遗留的口音。

这一切都应该大写你的第一个字母,并删除任何口音:)

我使用这种方法类似的情况,但我不知道如果问题要求使其他字母小写。

 - (NSString *)capitalizedOnlyFirstLetter { if (self.length < 1) { return @""; } else if (self.length == 1) { return [self capitalizedString]; } else { NSString *firstChar = [self substringToIndex:1]; NSString *otherChars = [self substringWithRange:NSMakeRange(1, self.length - 1)]; return [NSString stringWithFormat:@"%@%@", [firstChar uppercaseString], [otherChars lowercaseString]]; } } 

只是为了添加一些选项,我使用这个类别来大写NSString的第一个字母。

 @interface NSString (CapitalizeFirst) - (NSString *)capitalizeFirst; - (NSString *)removeDiacritic; @end @implementation NSString (CapitalizeFirst) - (NSString *)capitalizeFirst { if ( self.length <= 1 ) { return [self uppercaseString]; } else { return [[[[self substringToIndex:1] removeDiacritic] uppercaseString] stringByAppendingString:[[self substringFromIndex:1] removeDiacritic]]; // Or: return [NSString stringWithFormat:@"%@%@", [[[self substringToIndex:1] removeDiacritic] uppercaseString], [[self substringFromIndex:1] removeDiacritic]]; } } - (NSString *)removeDiacritic { // Taken from: http://stackoverflow.com/a/10932536/1986221 NSData *data = [NSData dataUsingEncoding:NSASCIIStringEncoding allowsLossyConversion:YES]; return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; } @end 

然后你可以简单地调用:

 NSString *helloWorld = @"hello world"; NSString *capitalized = [helloWorld capitalizeFirst]; NSLog(@"%@ - %@", helloWorld, capitalized); 

从iOS 9.0开始,有一种方法可以使用当前语言环境来大写string:

 @property(readonly, copy) NSString *localizedCapitalizedString;