53

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.

Christopher Wade Cantley
  • 7,122
  • 5
  • 35
  • 48

8 Answers8

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.

Slipp D. Thompson
  • 33,165
  • 3
  • 43
  • 43
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • I have not tried capitalizedStringWithLocale so I will play with it and comment back in a few minutes. – Christopher Wade Cantley Mar 25 '15 at 16:42
  • 2
    Thanks. Turns out the swift example on Apple's docs was mistyped which threw me for a loop (figuratively). – Christopher Wade Cantley Mar 25 '15 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.)_ – Slipp D. Thompson Mar 20 '23 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. :) – James Webster Mar 22 '23 at 07:59
38

Swift 3:

var lowercased = "hello there"

var stringCapitalized = lowercased.capitalized
//prints:  "Hello There"
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
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"
}
Evgenii
  • 36,389
  • 27
  • 134
  • 170
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"

Christopher Wade Cantley
  • 7,122
  • 5
  • 35
  • 48
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.

Narsail
  • 735
  • 1
  • 8
  • 12
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

Community
  • 1
  • 1
chirag90
  • 2,211
  • 1
  • 22
  • 37
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.

Christopher Wade Cantley
  • 7,122
  • 5
  • 35
  • 48
0

Swift 5 version of Christopher Wade's answer

let str = "my string"

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

print(result) // prints My String
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256