What is the localizedCapitalized property in Swift?

Overview

In Swift, the localizedCapitalized property returns the capitalized representation of a string instance that is produced using the current locale.

Syntax

string.localizedCapitalized

Return Value

This property returns a capitalized representation of a string using the current locale.

Example

// import the foundation framework
import Foundation
// create strings
let str1 = "google"
let str2 = "netflix"
let str3 = "amazon"
let str4 = "apple"
let str5 = "meta"
// get the capitalized representation
print(str1.localizedCapitalized)
print(str2.localizedCapitalized)
print(str3.localizedCapitalized)
print(str4.localizedCapitalized)
print(str5.localizedCapitalized)

Explanation

  • Line 2: We import the Foundation framework. This allows us to use the localizedCapitalized property.
  • Lines 5–9: We create some strings.
  • Lines 12–16: We use localizedCapitalized on the strings and print the output to the console.

Free Resources