swift - 如何使用Swift将句子的第一个字符大写

标签 swift capitalization

我有一个包含我的句子的字符串描述,并且只想将第一个字母大写。我尝试了不同的东西,但大多数都给我异常和错误。我正在使用 Xcode 6。

到目前为止,这是我尝试过的:

let cap = [description.substringToIndex(advance(0,1))] as String
    description = cap.uppercaseString + description.substringFromIndex(1)

它给了我:

Type 'String.Index' does not conform to protocol 'IntegerLiteralConvertible'

我试过:

 func capitalizedStringWithLocale(locale:0) -> String

但我还没有弄清楚如何让它发挥作用。

最佳答案

在 Swift 2 中,你可以这样做

String(text.characters.first!).capitalizedString + String(text.characters.dropFirst())

关于swift - 如何使用Swift将句子的第一个字符大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27082587/

相关文章:

java - 使用流 API 设置字符串全部小写但首字母大写

cocoa - 强制 NSTextField 中的所有大写字母(Cocoa)

mysql - 数据库中所有表的首字母大写

asp.net - 如果大小写不同,Chrome 会对同一 Assets 发出多个请求

javascript - 为什么 .toUpperCase() 不将 str[i] 大写?

ios - 在ARKit 1.5中进行初始检测后如何跟踪图像 anchor ?

ios - 是否可以在 draw(_ rect : CGRect) function in a UIView subclass that is backed by a CATiledLayer? 中在矩形边界外画一条线

ios - 如何使用导航 Controller 调用 View Controller

swift - 类型 'T' 不符合协议(protocol) 'EntityType'

ios - 带子节点的SKSpriteNode的触摸检测