如何使用 Swift iOS 将字符串中的每个单词大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29261218/
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
How to capitalize each word in a string using Swift iOS
提问by Christopher Wade Cantley
Is there a function to capitalize each word in a string or is this a manual process?
是否有一个函数可以将字符串中的每个单词大写,还是这是一个手动过程?
For e.g. "bob is tall" And I would like "Bob Is Tall"
例如“鲍勃很高”我想要“鲍勃很高”
Surely there is something and none of the Swift IOS answers I have found seemed to cover this.
当然有一些东西,我发现的 Swift IOS 答案似乎都没有涵盖这一点。
回答by James Webster
Are you looking for capitalizedString
你在找吗 capitalizedString
Discussion
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.
讨论
一个字符串,其中每个单词的第一个字符更改为其对应的大写值,其余所有字符都设置为其对应的小写值。
and/or capitalizedStringWithLocale(_:)
和/或 capitalizedStringWithLocale(_:)
Returns a capitalized representation of the receiver using the specified locale.
For strings presented to users, pass the current locale ([NSLocale currentLocale]). To use the system locale, pass nil.
使用指定的语言环境返回接收者的大写表示。
对于呈现给用户的字符串,传递当前语言环境([NSLocale currentLocale])。要使用系统区域设置,请传递 nil。
回答by Josh O'Connor
Swift 3:
斯威夫特 3:
var lowercased = "hello there"
var stringCapitalized = lowercased.capitalized
//prints: "Hello There"
回答by Evgenii
Since iOS 9 a localised capitalization function is available as capitalised letters may differ in languages.
由于 iOS 9 提供了本地化的大写功能,因为大写字母可能因语言而异。
if #available(iOS 9.0, *) {
"istanbul".localizedCapitalizedString
// In Turkish: "?stanbul"
}
回答by Christopher Wade Cantley
An example of the answer provided above.
上面提供的答案示例。
var sentenceToCap = "this is a sentence."
println(sentenceToCap.capitalizedStringWithLocale(NSLocale.currentLocale()) )
End result is a string "This Is A Sentence"
最终结果是一个字符串“This Is A Sentence”
回答by Narsail
For Swift 3it has been changed to capitalized
.
对于Swift 3,它已更改为capitalized
.
Discussion
This property performs the canonical (non-localized) mapping. It is suitable for programming operations that require stable results not depending on the current locale. 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 (listed under getLineStart(_:end:contentsEnd:for:)). Some common word delimiting punctuation isn't considered, so this property may not generally produce the desired results for multiword strings. Case transformations aren't guaranteed to be symmetrical or to produce strings of the same lengths as the originals. See lowercased for an example.
讨论
此属性执行规范(非本地化)映射。它适用于需要稳定结果而不依赖于当前语言环境的编程操作。大写字符串是一个字符串,其中每个单词的第一个字符更改为其对应的大写值,其余所有字符都设置为其对应的小写值。“单词”是由空格、制表符或行终止符分隔的任何字符序列(列在 getLineStart(_:end:contentsEnd:for:) 下)。一些常见的单词定界标点符号不被考虑,因此此属性通常不会为多词字符串产生所需的结果。不保证大小写转换是对称的或生成与原始字符串长度相同的字符串。有关示例,请参见小写。
回答by chirag90
There is a built in function for that
有一个内置的功能
nameOfString.capitalizedString
This will capitalize every word of string. To capitalize only the first letter you can use:
这将大写字符串的每个单词。要仅大写第一个字母,您可以使用:
nameOfString.replaceRange(nameOfString.startIndex...nameOfString.startIndex, with: String(nameOfString[nameOfString.startIndex]).capitalizedString)
回答by Christopher Wade Cantley
Here is what I came up with that seems to work but I am open to anything that is better.
这是我想出的似乎有效的方法,但我对任何更好的方法持开放态度。
func firstCharacterUpperCase(sentenceToCap:String) -> String {
//break it into an array by delimiting the sentence using a space
var breakupSentence = sentenceToCap.componentsSeparatedByString(" ")
var newSentence = ""
//Loop the array and concatinate the capitalized word into a variable.
for wordInSentence in breakupSentence {
newSentence = "\(newSentence) \(wordInSentence.capitalizedString)"
}
// send it back up.
return newSentence
}
or if I want to use this as an extension of the string class.
或者如果我想将其用作字符串类的扩展。
extension String {
var capitalizeEachWord:String {
//break it into an array by delimiting the sentence using a space
var breakupSentence = self.componentsSeparatedByString(" ")
var newSentence = ""
//Loop the array and concatinate the capitalized word into a variable.
for wordInSentence in breakupSentence {
newSentence = "\(newSentence) \(wordInSentence.capitalizedString)"
}
// send it back up.
return newSentence
}
}
Again, anything better is welcome.
再一次,任何更好的东西都是受欢迎的。