ios Swift 仅将 .uppercaseString 应用于字符串的第一个字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26306326/
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
Swift apply .uppercaseString to only the first letter of a string
提问by SomeGuy
I am trying to make an autocorrect system, and when a user types a word with a capital letter, the autocorrect doesn't work. In order to fix this, I made a copy of the string typed, applied .lowercaseString, and then compared them. If the string is indeed mistyped, it should correct the word. However then the word that replaces the typed word is all lowercase. So I need to apply .uppercaseString to only the first letter. I originally thought I could use
我正在尝试制作一个自动更正系统,当用户输入带有大写字母的单词时,自动更正不起作用。为了解决这个问题,我复制了输入的字符串,应用了 .lowercaseString,然后比较了它们。如果字符串确实输入错误,它应该更正单词。但是,替换输入的单词的单词都是小写的。所以我只需要将 .uppercaseString 应用于第一个字母。我原本以为我可以用
nameOfString[0]
but this apparently does not work. How can I get the first letter of the string to uppercase, and then be able to print the full string with the first letter capitalized?
但这显然不起作用。如何将字符串的第一个字母变为大写,然后能够打印首字母大写的完整字符串?
Thanks for any help!
谢谢你的帮助!
回答by Kirsteins
Including mutating and non mutating versions that are consistent with API guidelines.
包括符合 API 指南的变异和非变异版本。
Swift 3:
斯威夫特 3:
extension String {
func capitalizingFirstLetter() -> String {
let first = String(characters.prefix(1)).capitalized
let other = String(characters.dropFirst())
return first + other
}
mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}
Swift 4:
斯威夫特 4:
extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).uppercased() + self.lowercased().dropFirst()
}
mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}
回答by Leo Dabus
Swift 5.1 or later
Swift 5.1 或更高版本
extension StringProtocol {
var firstUppercased: String { prefix(1).uppercased() + dropFirst() }
var firstCapitalized: String { prefix(1).capitalized + dropFirst() }
}
Swift 5
斯威夫特 5
extension StringProtocol {
var firstUppercased: String { return prefix(1).uppercased() + dropFirst() }
var firstCapitalized: String { return prefix(1).capitalized + dropFirst() }
}
"Swift".first // "S"
"Swift".last // "t"
"hello world!!!".firstUppercased // "Hello world!!!"
"?".firstCapitalized // "?"
"?".firstCapitalized // "?"
"?".firstCapitalized // "?"
回答by Maselko
Swift 3.0
斯威夫特 3.0
for "Hello World"
对于“你好世界”
nameOfString.capitalized
or for "HELLO WORLD"
或“你好世界”
nameOfString.uppercased
回答by sam k
Swift 4.0
斯威夫特 4.0
string.capitalized(with: nil)
or
或者
string.capitalized
However this capitalizes first letter of every word
然而,这将大写每个单词的第一个字母
Apple's documentation:
苹果的文档:
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.
大写字符串是一个字符串,其中每个单词的第一个字符更改为其对应的大写值,其余所有字符都设置为其对应的小写值。“单词”是由空格、制表符或行终止符分隔的任何字符序列。一些常见的单词定界标点符号不被考虑,因此此属性通常不会为多词字符串产生所需的结果。有关其他信息,请参阅 getLineStart(_:end:contentsEnd:for:) 方法。
回答by user2260304
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()
For Swift 5:
对于 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())
}
}
回答by DURGESH
For first character in word use .capitalized
in swift and for whole-word use .uppercased()
用于单词中的第一个字符.capitalized
swift 和整个单词的使用.uppercased()
回答by Phil
Swift 2.0 (Single line):
Swift 2.0(单行):
String(nameOfString.characters.prefix(1)).uppercaseString + String(nameOfString.characters.dropFirst())
回答by oscar castellon
Swift 3 (xcode 8.3.3)
斯威夫特 3 (xcode 8.3.3)
Uppercase all first characters of string
大写字符串的所有第一个字符
let str = "your string"
let capStr = str.capitalized
//Your String
Uppercase all characters
大写所有字符
let str = "your string"
let upStr = str.uppercased()
//YOUR STRING
Uppercase only first character of string
仅大写字符串的第一个字符
var str = "your string"
let capStr = String(str.characters.prefix(1)).uppercased() + String(str.characters.dropFirst())
//Your string
回答by Sanjay Mishra
In swift 5
在迅捷 5
https://www.hackingwithswift.com/example-code/strings/how-to-capitalize-the-first-letter-of-a-string
https://www.hackingwithswift.com/example-code/strings/how-to-capitalize-the-first-letter-of-a-string
extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).capitalized + dropFirst()
}
mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}
use with your string
与您的字符串一起使用
let test = "the rain in Spain"
print(test.capitalizingFirstLetter())
回答by Yura
From Swift 3 you can easily use
textField.autocapitalizationType = UITextAutocapitalizationType.sentences
从 Swift 3 开始,您可以轻松使用
textField.autocapitalizationType = UITextAutocapitalizationType.sentences