我正在尝试制作一个自动更正系统,当用户输入一个大写字母的单词时,自动更正不起作用。为了解决这个问题,我复制了键入的字符串,应用了. lowercasestring,然后比较它们。如果字符串确实输入错误,则应该更正单词。然而,取而代之的单词都是小写的。所以我需要对第一个字母应用. uppercasestring。我本来觉得我可以用
nameOfString[0]
但这显然行不通。我怎么能得到字符串的第一个字母大写,然后能够打印完整的字符串与第一个字母大写?
谢谢你的帮助!
我正在尝试制作一个自动更正系统,当用户输入一个大写字母的单词时,自动更正不起作用。为了解决这个问题,我复制了键入的字符串,应用了. lowercasestring,然后比较它们。如果字符串确实输入错误,则应该更正单词。然而,取而代之的单词都是小写的。所以我需要对第一个字母应用. uppercasestring。我本来觉得我可以用
nameOfString[0]
但这显然行不通。我怎么能得到字符串的第一个字母大写,然后能够打印完整的字符串与第一个字母大写?
谢谢你的帮助!
当前回答
斯威夫特4.0
string.capitalized(with: nil)
or
string.capitalized
但是,每个单词的首字母都要大写
苹果公司的文档:
A capitalized string is a string with the first character in each word changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values. A “word” is any sequence of characters delimited by spaces, tabs, or line terminators. Some common word delimiting punctuation isn’t considered, so this property may not generally produce the desired results for multiword strings. See the getLineStart(_:end:contentsEnd:for:) method for additional information.
其他回答
结合上面的答案,我写了一个小扩展,大写每个单词的第一个字母(因为这是我正在寻找的,并认为其他人可以使用它)。
我谦卑地提出:
extension String {
var wordCaps:String {
let listOfWords:[String] = self.componentsSeparatedByString(" ")
var returnString: String = ""
for word in listOfWords {
if word != "" {
var capWord = word.lowercaseString as String
capWord.replaceRange(startIndex...startIndex, with: String(capWord[capWord.startIndex]).uppercaseString)
returnString = returnString + capWord + " "
}
}
if returnString.hasSuffix(" ") {
returnString.removeAtIndex(returnString.endIndex.predecessor())
}
return returnString
}
}
Leonardo Savio Dabus:
我想大多数用例是获得适当的套管:
import Foundation
extension String {
var toProper:String {
var result = lowercaseString
result.replaceRange(startIndex...startIndex, with: String(self[startIndex]).capitalizedString)
return result
}
}
我用柯尔斯泰因斯解得到了第一个复制的字符。这将大写第一个字符,不会看到双字符:
var s: String = "hello world"
s = prefix(s, 1).capitalizedString + suffix(s, countElements(s) - 1)
我不知道它的效率是高还是低,我只知道它给了我想要的结果。
extension String {
func firstCharacterUpperCase() -> String? {
let lowercaseString = self.lowercaseString
return lowercaseString.stringByReplacingCharactersInRange(lowercaseString.startIndex...lowercaseString.startIndex, withString: String(lowercaseString[lowercaseString.startIndex]).uppercaseString)
}
}
let x = "heLLo"
let m = x.firstCharacterUpperCase()
对于Swift 5:
extension String {
func firstCharacterUpperCase() -> String? {
guard !isEmpty else { return nil }
let lowerCasedString = self.lowercased()
return lowerCasedString.replacingCharacters(in: lowerCasedString.startIndex...lowerCasedString.startIndex, with: String(lowerCasedString[lowerCasedString.startIndex]).uppercased())
}
}
斯威夫特4.0
string.capitalized(with: nil)
or
string.capitalized
但是,每个单词的首字母都要大写
苹果公司的文档:
A capitalized string is a string with the first character in each word changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values. A “word” is any sequence of characters delimited by spaces, tabs, or line terminators. Some common word delimiting punctuation isn’t considered, so this property may not generally produce the desired results for multiword strings. See the getLineStart(_:end:contentsEnd:for:) method for additional information.