To convert the first character in a string to uppercase in Swift, you can use string manipulation methods such as prefix()
and uppercased()
.
In this example,
str1
with a value of "hello world"
.str1
using the prefix()
method.uppercased()
method.let str1 = "hello world"
let modifiedStr1 = str1.prefix(1).uppercased() + str1.dropFirst()
print("Modified string is:", modifiedStr1)
Modified string is: Hello world
In this example,
str2
with a value of "swift"
.str2
using the prefix()
method.uppercased()
method.let str2 = "swift"
let modifiedStr2 = str2.prefix(1).uppercased() + str2.dropFirst()
print("Modified string is:", modifiedStr2)
Modified string is: Swift
In this tutorial, we learned How to convert First Character in String to Uppercase in Swift language with well detailed examples.