Get the length of a string in Swift

In this tutorial, I will show you how you can get the length of a string in Swift. A string in Swift is a collection of characters surrounded by double quotation marks. We have several options available to get the length of a string.

Now, have a look at the methods below to achieve this task

  • Using the count property
  • Using the NSString class
  • Using the unicodeScalars property
  • Using for loop

Let’s explore the method one by one to extract the length of a Swift string.

Using the count property

In Swift, the count property is used to determine the number of elements or characters in a collection, such as an array or a string. It provides a convenient way to get the length or size of a collection.
It is a simple and mostly used method.

Now, have a look at the example below.

let string = "CodeSpeedy"
let length = string.count
print("String length is \(length)")

The above program initializes a variable string with the value “CodeSpeedy” and then calculates the length of the string using the count property.

Output:

String length is 10

Using the NSString class

In Swift, NSString is a class that represents an immutable string of characters. It is actually part of the Foundation framework, which is a set of Objective-C classes bridged to Swift. NSString is a direct mapping of the Objective-C class with the same name.

import Foundation

let string = "CodeSpeedy" 
let length = NSString(string: string).length
print("String length is \(length)")

In the above program, I have imported Foundation framework to use NSString class.
After that, I have created an NSString object by passing the variable string to the NSString initializer.
Then I have accessed the length property of the NSString object to obtain the length of the string.

Output:

String length is 10

Using the unicodeScalars property

We can determine the length of a string in Swift by using the unicodeScalars property. This property provides a collection of Unicode scalar values that constitute the string. We can then count the number of elements in this collection to find the string’s length.

Example

let string = "CodeSpeedy"
let length = string.unicodeScalars.count
print("String length is \(length)")

Output:

String length is 10

Using for loop

We can use for loop to get the length of a string in Swift. Even though there are easier built-in ways to get the length of a string, it’s a good idea to try different approaches to solve a problem.

We can apply custom logic in this method to get the length of a string.

Example

let str = "Welcome to CodeSpeedy"
var strLength = 0

for _ in str {
    strLength += 1
}

print("String length is \(strLength)")

 

The above code will calculate and print the length (number of characters) of the string “Welcome to CodeSpeedy.”

I have used a for-in loop to iterate through each character in the string, incrementing the variable strLength by 1 for each character. After the loop, it will print the value of strLength, which is the length of the string.

Output:

String length is 21

Leave a Reply

Your email address will not be published. Required fields are marked *