Swift – String Length/Count

In this tutorial, we will learn to find the length of string in Swift programming.

To get the length of a String in Swift, use count property of the string.

string.count

count property is an integer value representing the number of characters in this string.

Example 1 – String length

In the following example, we take a string in variable str and find its length using count property.

main.swift

var str = "Hello World"
var len = str.count
print( "Length of str is \(len)" )

Output

Length of str is 11
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we have learned to get the length of a String using count property with the help of Swift example programs.