Skip to content
DeveloperMemos

Capitalizing With Swift's capitalized

Swift, String Manipulation, Capitalization1 min read

In Swift, string manipulation is a common task when working with textual data. There are several built-in methods that facilitate modifying strings according to specific requirements. One such method is capitalized, which allows you to capitalize the first letter of each word in a string. This article will delve into the details of capitalized and provide practical examples to demonstrate its usage.

The capitalized Method

In Swift, the capitalized method is available on instances of the String class, and it provides a convenient way to capitalize the first letter of each word within a string. When called on a string, this method returns a new string with the desired capitalization applied.

The capitalized method follows a specific set of rules for capitalizing words within a string:

  • The first character of every word is converted to uppercase.
  • All other characters in each word are converted to lowercase.

Let's dive into some examples to see how the capitalized method works in practice.

Example 1: Capitalizing a Simple String

1let input = "hello, world!"
2let capitalizedString = input.capitalized
3
4print(capitalizedString) // Output: Hello, World!

In this example, we start with a simple string, "hello, world!". By applying the capitalized method, the result becomes "Hello, World!", with the first letter of each word capitalized.

Example 2: Capitalizing Multiple Words

1let input = "japan is revolutionizing artificial intelligence"
2let capitalizedString = input.capitalized
3
4print(capitalizedString)
5// Output: Japan Is Revolutionizing Artificial Intelligence

In this example, we have a string containing multiple words. By using the capitalized method, we obtain a new string where the first letter of each word is capitalized.

Example 3: Handling Acronyms and Initialisms

1let input = "usa, nasa, html, ai"
2let capitalizedString = input.capitalized
3
4print(capitalizedString) // Output: Usa, Nasa, Html, Ai

The capitalized method also handles acronyms and initialisms correctly. In this example, the string "usa, nasa, html, ai" contains various acronyms and initialisms. By applying the capitalized method, the output retains the capitalization of these abbreviations: "Usa, Nasa, Html, Ai".

Example 4: Capitalizing Empty Strings

1let input = ""
2let capitalizedString = input.capitalized
3
4print(capitalizedString) // Output:

When the capitalized method is called on an empty string, it returns an empty string as well.

Example 5: Handling Non-Latin Characters

1let input = "こんにちは、世界!"
2let capitalizedString = input.capitalized
3
4print(capitalizedString) // Output: こんにちは、世界!

The capitalized method is not limited to Latin characters and can handle strings with non-Latin characters such as Japanese or Chinese. In this example, the string "こんにちは、世界!" remains unchanged because the capitalized method only capitalizes Latin characters.

In Closing

The capitalized method in Swift provides a straightforward way to capitalize the first letter of each word within a string. It is essential to keep in mind the specific rules followed by this method, especially when dealing with acronyms, initialisms, or non-Latin characters. By understanding and utilizing the capitalized method effectively, you can enhance your string manipulation capabilities in Swift.