How to capitalize the first letter in Swift

⋅ 2 min read ⋅ Swift String

Table of Contents

How to capitalize the first letter in each word

Swift string has the built-in string property, capitalized, that uppercase the first character in each word.

capitalized returns the new string with:

  1. The first character in each word changed to its corresponding uppercase value.
  2. All remaining characters set to their corresponding lowercase values.

Here is an example.

let hello = "hello, world!"
let shout = "UPPERCASED"
let lorem = "Itaque enim excepturi placeat aperiam sunt molestiae sint ea."

print(hello.capitalized)
// Hello, World!
print(shout.capitalized)
// Uppercased
print(lorem.capitalized)
// Itaque Enim Excepturi Placeat Aperiam Sunt Molestiae Sint Ea.

As you can see, capitalized property, capitalized every word. If you want to capitalize only the first letter of the first word in a sentence you have to do it yourself.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

How to capitalize the first letter in a sentence

There are many ways to do this, but I will show you one way.

I will create a new computed property for String called capitalizedSentence. And here is its behavior:

  1. The first character in each sentence changed to its corresponding uppercase value.
  2. All remaining characters set to their corresponding lowercase values.
extension String {
var capitalizedSentence: String {
// 1
let firstLetter = self.prefix(1).capitalized
// 2
let remainingLetters = self.dropFirst().lowercased()
// 3
return firstLetter + remainingLetters
}
}

1 First, we use the prefix(1) method to get the first letter of the string. Then, we capitalized it.
2 We remove the first letter using dropFirst() to get the remaining letters. Then, we lowercased them with lowercased().
3 Then, we combine the capitalized letter and the remaining lettters back together.

Here is an example of the same set of strings.

let hello = "hello, world!"
let shout = "UPPERCASED"
let lorem = "Itaque enim excepturi placeat aperiam sunt molestiae sint ea."

print(hello.capitalizedSentence)
// Hello, world!
print(shout.capitalizedSentence)
// Uppercased
print(lorem.capitalizedSentence)
// Itaque enim excepturi placeat aperiam sunt molestiae sint ea.

Caveat

The implementation I show you in this article is a basic one.

  • It only works with a string that contains one sentence. The second sentence won't get capitalized.
let lorem = "doloremque nostrum. voluptas autem."

print(lorem.capitalizedSentence)
// Doloremque nostrum. voluptas autem.
  • It doesn't work if the first string is a whitespace character and delimiter.
let hello = " hello"

print(hello.capitalizedSentence)
// hello

I think you can take it from here and modify it to suit your need.


Read more article about Swift, String, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
SwiftUI Gauge

iOS 16 brings a new view to SwiftUI, Gauge. Let's see what it is and how to use it.

Next
SwiftUI AnyLayout - smooth transitions between layout types

In iOS 16, SwiftUI got a new tool, AnyLayout, that makes it possible to transition between layouts while maintaining the identity of the views.

← Home