Swift Strings

Placement-ready Courses: Enroll Now, Thank us Later!

Strings are a fundamental data type in Swift. Swift offers a useful set of features for utilizing strings. It provides methods for string manipulation, formatting and searching. In this article, we’ll discuss strings and concepts related to them with the help of relevant examples.

Swift String Creation

We can create a string by either using a string literal or defining a string instance.

var stringExample: String = "DataFlair"
print(stringExample)

Output:

DataFlair

The example below shows how to create a string instance.

var stringInstance = String()

Empty String

We can create a string by either using an empty string literal or defining a string instance.

var stringExample: String = ""

The example below shows how to create a string instance.

var stringInstance = String()

String Interpolation

We can also embed variables directly within a string using string interpolation. It becomes easy to format and display variable values alongside text.

var name: String = "DataFlair"
print( "Name: \(name)")

Output:

Name: DataFlair

String Length

Swift provides a property count to measure the length of the string

var stringExample = "DataFlair"
print(stringExample.count)

Output:

9

String Comparison

To compare strings, we use the ‘==’ operator. When the strings are the same, the condition is true, otherwise false.

var stringExample1 = "DataFlair"
var stringExample2 = "DataFlair"
var stringExample3 = "DataFlair 1"

print(stringExample1 == stringExample2)
print(stringExample2 == stringExample3)
print(stringExample2 != stringExample3)

Output:

true
false
true

String Iteration in Swift

We use for-in loop to iterate through each character in a string.

var stringExample1 = "DataFlair"

for char in stringExample1{
    print(char, terminator: ".")
}

Output:

D.a.t.a.F.l.a.i.r.

Swift String Concatenation

We can concatenate two strings in two ways. One is using the append method. The second uses the + operator.

The following examples show the usage of concatenation methods.

var string1 = "Data"
var string2 = "Flair"
var stringExample = string1 + string2
print("Using + operator:", stringExample)
string1.append(string2)
print("Using append function:", string1)

Output:

Using + operator: DataFlair
Using append function: DataFlair

String Methods in Swift

MethodDescription
isEmptyCheck if the string is empty
capitalizedCapitalize the first letter of every word in the string
hasPrefix(prefix)Check if particular characters are at the start of the string
hasSuffix(suffix)Checks if particular characters are at the end of the string
lowercased()Converts the string into lowercase
uppercased()Converts the string into uppercase
reversed()Reverses the string

Escape Sequences in Swift

Strings cannot have unescaped characters like a backslash (\) or double quotes (“). These special characters can be written in a string literal using escape sequences.

The table below represents the escape sequence for the character, along with its description.

Escape Sequence CharacterEscape Sequence
Null Character\0
Back Slash\\
Backspace \b
Form Feed\f
Newline\n
Carriage Return\r
Horizontal Tab\t
Vertical Tab\v
Single Quote\‘
Double Quote\“
Octal number\000
Hexadecimal number\xhh

Unicode Strings in Swift

We can iterate through the UTF-8 and UTF-16 representation of the strings. We use the utf8 and utf16 properties.

var example = "DataFlair"

print("UTF-16")
for ch in example.utf16{
    print(ch)
}
print()
print("UTF-8")
for ch in example.utf8{
    print(ch)
}

Output:

UTF-16
68
97
116
97
70
108
97
105
114

UTF-8
68
97
116
97
70
108
97
105
114

Conclusion

Strings are an essential data type in any language. They represent textual data. In this article, we’ve covered swift strings and concepts related to them. We have also seen how we can compare strings and use them in programming. We have discussed different functions and operations provided by Swift.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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