In this tutorial, you will learn how to get the length of a string in Swift programming with the help of an example.
It is quite a simple task to find the number of characters of a string in Swift. Swift already has an in-built count
property. The Swift count
property can count the characters of a string.
Below is the syntax of how you can use the count
property for the task:
string.count
Now see a proper example of finding the character length in a string:
var myString = "swiftspeedy"
var length = myString.count
print(length)
In the above code, we store our string in a variable myString
. After that, we have applied the count property to the string type variable myString
.
After we run our program, we can see the output given below:
11
If you notice, you will find our program is showing the exact number of characters in the string myString
.
That’s it…
From the output, we can verify that we are able to find the exact length of a Swift string successfully. We did it in a simple way.
Also, read: How to Generate a random Password in Swift?