ios NSString 中的大写首字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7359362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uppercase first letter in NSString
提问by Nielsou Hacken-Bergen
How can I uppercase the fisrt letter of a NSString, and removing any accents ?
如何将 NSString 的第一个字母大写,并删除任何重音符号?
For instance, àlter
, Alter
, alter
should becomeAlter
.
例如,àlter
,Alter
,alter
应该变成Alter
。
But, /lter
, )lter
, :lter
should remains the same, as the first character is not a letter.
但是,/lter
,)lter
,:lter
应该仍然是相同的,作为第一个字符是不是字母。
回答by Vijay-Apple-Dev.blogspot.com
Please Do NOT use this method. Because one letter may have different count in different language. You can check dreamlax answer for that. But I'm sure that You would learn something from my answer. Happy coding :)
请不要使用这种方法。因为一个字母在不同的语言中可能有不同的计数。您可以查看 Dreamlax 的答案。但我相信你会从我的回答中学到一些东西。快乐编码:)
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.
}
Why should I care about the number of letters?
我为什么要关心字母的数量?
If you try to access (e.g NSMakeRange
, substringToIndex
etc)
the first character in an empty string like @""
, then your app will crash. To avoid this you must verify that it exists before processing on it.
如果您尝试访问(例如NSMakeRange
,substringToIndex
等)空字符串中的第一个字符,例如@""
,则您的应用程序将崩溃。为避免这种情况,您必须在对其进行处理之前验证它是否存在。
What if my string was nil?
如果我的字符串为零怎么办?
Mr.Nil: I'm 'nil'.I can digest anything that you send to me. I won't allow your app to crash all by itself. ;)
Mr.Nil: 我是'nil'。我能消化你寄给我的任何东西。我不会让您的应用程序自行崩溃。;)
nil
will observe any method call you send to it.
nil
将观察您发送给它的任何方法调用。
So it will digest anything you try on it, nil
is your friend.
所以它会消化你试穿的任何东西,nil
是你的朋友。
回答by meaning-matters
You can use NSString
's:
您可以使用NSString
的:
- (NSString *)capitalizedString
- (NSString *)capitalizedString
or (iOS 6.0 and above):
或(iOS 6.0 及以上):
- (NSString *)capitalizedStringWithLocale:(NSLocale *)locale
- (NSString *)capitalizedStringWithLocale:(NSLocale *)locale
回答by dreamlax
Since you want to remove diacritic marks, you could use this methodin combination with the common string manipulating methods, like this:
由于要删除变音符号,可以将此方法与常见的字符串操作方法结合使用,如下所示:
/* create a locale where diacritic marks are not considered important, e.g. 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]];
回答by ASLLOP
Since iOS 9.0 there is a method to capitalize string using current locale:
从 iOS 9.0 开始,有一种使用当前语言环境将字符串大写的方法:
@property(readonly, copy) NSString *localizedCapitalizedString;
回答by Madhu
Gonna drop a list of steps which I think you can use to get this done. Hope you can follow through without a prob! :)
将删除我认为您可以用来完成此操作的步骤列表。希望你能顺利完成!:)
- Use
decomposedStringWithCanonicalMapping
to decompose any accents (Important to make sure accented characters aren't just removed unnecessarily) - Use characterAtIndex: to extract the first letter (index 0), use
upperCaseString
to turn it into capitol lettering and usestringByReplacingCharactersInRange
to replace the first letter back into the original string. - In this step, BEFORE turning it into uppercase, you can check whether the first letter is one of the characters you do not want to replace, e.g. ":" or ";", and if it is, do not follow through with the rest of the procedure.
- Do a
[theString stringByReplacingOccurrencesOfString:@"
" withString:@""]` sort of call to remove any accents left over.
- 使用
decomposedStringWithCanonicalMapping
以分解任何口音(重要,以确保重音的字符不只是删除不必要的) - 使用 characterAtIndex: 提取第一个字母(索引 0),使用
upperCaseString
将其转换为stringByReplacingCharactersInRange
大写字母并使用将第一个字母替换回原始字符串。 - 在这一步中,在将其转成大写之前,您可以检查第一个字母是否是您不想替换的字符之一,例如“:”或“;”,如果是,则不要继续执行其余的字符的程序。
- 执行
[theString stringByReplacingOccurrencesOfString:@"
“ withString:@""]` 类型的调用以删除任何剩余的重音符号。
This all should both capitalize your first letter AND remove any accents :)
这一切都应该大写您的第一个字母并删除任何重音:)
回答by Josip B.
I'm using this method for similar situations but I'm not sure if question asked to make other letters lowercase.
我在类似情况下使用这种方法,但我不确定是否要求将其他字母设为小写。
- (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]];
}
}
回答by Alejandro Iván
Just for adding some options, I use this category to capitalize the first letter of a NSString
.
只是为了添加一些选项,我使用这个类别来将 a 的第一个字母大写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
And then you can simply call:
然后你可以简单地调用:
NSString *helloWorld = @"hello world";
NSString *capitalized = [helloWorld capitalizeFirst];
NSLog(@"%@ - %@", helloWorld, capitalized);