0

I'm getting strings from a server that is all caps like:

"HELLO WORLD"

but I'm trying to make is so each word is caps on its own like:

"Hello World"

I've tried this:

extension String {
    func capitalizingFirstLetter() -> String {
        let first = String(characters.prefix(1)).capitalized
        let other = String(characters.dropFirst()).lowercased()
        return first + other
    }

    mutating func capitalizeFirstLetter() {
        self = self.capitalizingFirstLetter()
    }
}

but the result is

"Hello world"

any idea on how to do this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92

1 Answers1

1

Apple already did that for you:

print("HELLO WORLD".capitalized)

Documentation: https://developer.apple.com/documentation/foundation/nsstring/1416784-capitalized

Code Different
  • 90,614
  • 16
  • 144
  • 163