55

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.

2
  • 1
    var someString = "lol" ; someString.capitalized // "Lol"
    – Khaledonia
    Commented Oct 5, 2017 at 8:09
  • A few others detailed this option below. ;) Commented Oct 10, 2017 at 13:57

8 Answers 8

62

Are you looking for capitalized

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 capitalized(with locale:) (see also: localizedCapitalized)

Returns a capitalized representation of the receiver using the specified locale.

For strings presented to users, pass the current locale (Locale.current). To use the system locale, pass nil.

10
  • I have not tried capitalizedStringWithLocale so I will play with it and comment back in a few minutes. Commented Mar 25, 2015 at 16:42
  • 2
    Thanks. Turns out the swift example on Apple's docs was mistyped which threw me for a loop (figuratively). Commented Mar 25, 2015 at 16:54
  • In newer versions of Swift, this is now just capitalized, capitalized(with: Locale?), and localizedCapitalized. In Objective-C the names are still the same as above. (The long story short is that in Obj-C it's still convention to suffix a property that returns an instance of that type with a short name of that type, e.g. …String— but not so in Swift and the Swift compiler automatically strips that suffix from property names bridged from Obj-C to Swift.) Commented Mar 20, 2023 at 16:54
  • @SlippD.Thompson That's useful and comprehensive information which provides a much needed update to an out of date answer. You should feel free to edit answers you see like this to update them. :) Commented Mar 22, 2023 at 7:59
  • And what if you don't want the remaining characters lowercased?
    – malhal
    Commented Dec 5, 2023 at 23:40
41

Swift 3:

var lowercased = "hello there"

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

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"
}
1
  • 2
    For Swift, it's localizedCapitalized, for ObjC, it's localizedCapitalizedString. Commented Sep 16, 2020 at 12:29
12

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"

0
8

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.

5

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

1
  • I tried this, and it didn't work for me. It just seemed to cap only the first word. I found the thread that recommended this. Commented Mar 25, 2015 at 16:35
1

Swift 5 version of Christopher Wade's answer

let str = "my string"

let result = str.capitalized(with: NSLocale.current)

print(result) // prints My String
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.