资本化第一句话(斯威夫特)

时间:2014-11-22 21:09:06

标签: swift capitalization

我有一个字符串描述来保存我的句子。我只想把第一个字母大写。我尝试了不同的东西,但大多数都给了我例外和错误。我正在使用Xcode 6。

这是我到目前为止所尝试的内容

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

它给了我:Type' String.Index'不符合协议' IntegerLiteralConvertible'

我试过

 func capitalizedStringWithLocale(locale:0) -> String

但我无法弄清楚如何让它发挥作用。 有什么想法吗?

9 个答案:

答案 0 :(得分:8)

在Swift 2中,您可以执行String(text.characters.first!).capitalizedString + String(text.characters.dropFirst())

答案 1 :(得分:4)

Swift 3中的另一种可能性 -

extension String {
    func capitalizeFirst() -> String {
        let firstIndex = self.index(startIndex, offsetBy: 1)
        return self.substring(to: firstIndex).capitalized + self.substring(from: firstIndex).lowercased()
    }
}

编辑Swift 4
来自Swift 3代码的警告 -

  不推荐使用'substring(to :)':请使用字符串切片下标   使用“部分范围向上”运算符。
  'substring(from :)'已弃用:请使用字符串切片下标和'partial range from'运算符。

Swift 4解决方案 -

extension String {
    var capitalizedFirst: String {
        guard !isEmpty else {
            return self
        }

        let capitalizedFirstLetter  = charAt(i: 0).uppercased()
        let secondIndex             = index(after: startIndex)
        let remainingString         = self[secondIndex..<endIndex]

        let capitalizedString       = "\(capitalizedFirstLetter)\(remainingString)"
        return capitalizedString
    }
}

答案 2 :(得分:2)

import Foundation

// A lowercase string
let description = "the quick brown fox jumps over the lazy dog."

// The start index is the first letter
let first = description.startIndex

// The rest of the string goes from the position after the first letter
// to the end.
let rest = advance(first,1)..<description.endIndex

// Glue these two ranges together, with the first uppercased, and you'll
// get the result you want. Note that I'm using description[first...first]
// to get the first letter because I want a String, not a Character, which
// is what you'd get with description[first].
let capitalised = description[first...first].uppercaseString + description[rest]

// Result: "The quick brown fox jumps over the lazy dog."

你可能想要确保在开始之前句子中至少有一个字符,否则你会在尝试将索引超出字符串末尾时遇到运行时错误。

答案 3 :(得分:1)

以下是如何在Swift 4中执行此操作;以防万一它能帮助任何人:

extension String {
    func captalizeFirstCharacter() -> String {
        var result = self

        let substr1 = String(self[startIndex]).uppercased()
        result.replaceSubrange(...startIndex, with: substr1)

        return result
    }
}

它不会改变原始的String

答案 4 :(得分:1)

extension String {
var capitalizedFirstLetter:String {
    let string = self
    return string.replacingCharacters(in: startIndex...startIndex, with: String(self[startIndex]).capitalized)
}

}

let newSentence = sentence.capitalizedFirstLetter

答案 5 :(得分:0)

对于字符串中的一个或每个单词,您可以使用String的.capitalized属性。

print("foo".capitalized) //prints: Foo

print("foo foo foo".capitalized) //prints: Foo Foo Foo

答案 6 :(得分:0)

Swift 4.0最简单的解析

添加为计算属性扩展名

extension String {
    var firstCapitalized: String {
        var components = self.components(separatedBy: " ")
        guard let first = components.first else {
            return self
        }
        components[0] = first.capitalized
        return components.joined(separator: " ")
    }
}

用法

"hello world".firstCapitalized

答案 7 :(得分:0)

Swift 4.2版本:

extension String {

    var firstCharCapitalized: String {
        switch count {
        case 0:
            return self
        case 1:
            return uppercased()
        default:
            return self[startIndex].uppercased() + self[index(after: startIndex)...]
        }
    }
}

答案 8 :(得分:0)

Swift 5.0

答案1:

extension String {
    func capitalizingFirstLetter() -> String {
        return prefix(1).capitalized + dropFirst()
     }

    mutating func capitalizeFirstLetter() {
        self = self.capitalizingFirstLetter()
    }
 }

答案2:

 extension String {
       func capitalizeFirstLetter() -> String {
            return self.prefix(1).capitalized + dropFirst()
       }
  }

答案3:

 extension String {
       func capitalizeFirstLetter:String {
            return self.prefix(1).capitalized + dropFirst()
       }
  }
相关问题