यहां मैं जो काम करता हूं उसके साथ आता है लेकिन मैं बेहतर कुछ भी खोल रहा हूं।
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
}
या यदि मैं इसे स्ट्रिंग क्लास के विस्तार के रूप में उपयोग करना चाहता हूं।
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
}
}
फिर से, कुछ भी बेहतर स्वागत है।
स्रोत
2015-03-25 16:38:11
वर someString = "lol"; someString.capitalized // "lol" – Alexander
कुछ अन्य लोगों ने नीचे इस विकल्प का विस्तृत विवरण दिया। ;) –