In Swift, you can use array-like indexing or the prefix
method to get the first character of a string.
In this example,
str
with a value of "Hello World"
.startIndex
on the string str
or the prefix
method with argument 1
to get the first character.let str = "Hello World"
let firstChar = str[str.startIndex]
print("First character: \(firstChar)")
First character: H
In this example,
str
with a value of "apple"
.startIndex
on the string str
or the prefix
method with argument 1
to get the first character.let str = "apple"
let firstChar = str[str.startIndex]
print("First character: \(firstChar)")
First character: a
In this tutorial, we learned How to get First Character in String in Swift language with well detailed examples.