NSString 中的第一个字母大写
如何将 NSString 的第一个字母大写,并删除任何重音符号?
例如,Àlter
、Alter
、alter
应该变成 Alter
。
但是,/lter
、)lter
、:lter
应保持相同,因为第一个字符不是信。
How can I uppercase the fisrt letter of a NSString, and removing any accents ?
For instance, Àlter
, Alter
, alter
should become Alter
.
But, /lter
, )lter
, :lter
should remains the same, as the first character is not a letter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请不要使用此方法。因为一个字母在不同的语言中可能有不同的计数。您可以检查 dreamlax 答案。但我确信您会从我的回答中学到一些东西。
我为什么要关心字母数量?
如果您尝试访问(例如
NSMakeRange
、substringToIndex
等)空字符串中的第一个字符,例如
@""
,那么您的应用程序将崩溃。为了避免这种情况,您必须在对其进行处理之前验证它是否存在。如果我的字符串为零怎么办?
Mr.Nil:我是“无”。我可以消化你发送给我的任何内容。我不会允许你的应用程序自行崩溃。 ;)
nil
将观察您发送给它的任何方法调用。所以它会消化你尝试的任何东西,
nil
是你的朋友。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.
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.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. ;)
nil
will observe any method call you send to it.So it will digest anything you try on it,
nil
is your friend.您可以使用
NSString
:- (NSString *)capitalizedString
或 (iOS 6.0 及更高版本):
- (NSString *)capitalizedStringWithLocale:(NSLocale *)语言环境
You can use
NSString
's:- (NSString *)capitalizedString
or (iOS 6.0 and above):
- (NSString *)capitalizedStringWithLocale:(NSLocale *)locale
由于您想删除变音符号,因此可以使用 此方法与常见的字符串操作方法结合使用,如下所示:
Since you want to remove diacritic marks, you could use this method in combination with the common string manipulating methods, like this:
我将列出一个步骤列表,我认为您可以使用这些步骤来完成此任务。希望您能顺利完成! :)
decomposedStringWithCanonicalMapping
分解任何重音符号(重要的是要确保重音字符不会被不必要地删除)upperCaseString
将其转换为大写字母并使用stringByReplacingCharactersInRange
将第一个字母替换回原始字符串。[theString stringByReplacingOccurrencesOfString:@"
" withString:@""]` 类调用以删除剩余的重音符号。这一切都应该大写你的第一个字母并删除任何重音:)
Gonna drop a list of steps which I think you can use to get this done. Hope you can follow through without a prob! :)
decomposedStringWithCanonicalMapping
to decompose any accents (Important to make sure accented characters aren't just removed unnecessarily)upperCaseString
to turn it into capitol lettering and usestringByReplacingCharactersInRange
to replace the first letter back into the original string.[theString stringByReplacingOccurrencesOfString:@"
" withString:@""]` sort of call to remove any accents left over.This all should both capitalize your first letter AND remove any accents :)
从 iOS 9.0 开始,有一种使用当前语言环境将字符串大写的方法:
Since iOS 9.0 there is a method to capitalize string using current locale:
我在类似的情况下使用此方法,但我不确定问题是否要求将其他字母小写。
I'm using this method for similar situations but I'm not sure if question asked to make other letters lowercase.
只是为了添加一些选项,我使用此类别将
NSString
的第一个字母大写。然后你可以简单地调用:
Just for adding some options, I use this category to capitalize the first letter of a
NSString
.And then you can simply call: