How to capitalize each word in a string using Swift iOS

7 Solutions Collect From Internet About “How to capitalize each word in a string using Swift iOS”

Are you looking for 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(_:)

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.

Since iOS 9 a localised capitalization function is available as capitalised letters may differ in languages.

if #available(iOS 9.0, *) {
  "istanbul".localizedCapitalizedString
  // In Turkish: "İstanbul"
}

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”

For Swift 3 it has been changed to 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.

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)

Older Thread

Swift 3:

var lowercased = "hello there"

var stringCapitalized = lowercased.capitalized
//prints:  "Hello There"

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.