Waldo sessions now support scripting! – Learn more
App Development

How to Append String in Swift With Examples

Keshav Malik
Keshav Malik
How to Append String in Swift With Examples
November 22, 2022
6
min read

Swift is a high-level, compiled programming language developed by Apple for iOS, macOS, watchOS, Linux, and tvOS. Swift is designed to work with Apples Cocoa and the large body of the Objective-C code written for Apple products.

In iOS programming, you sometimes need to append string in Swift. Often, the task is the same, but the parameters may change. There are many ways to append a string in Swift and so it can be quite confusing to find the correct way to write Swift code. In this article, you’ll learn different ways to append a string in Swift and see some examples.

Strings are powerful and intuitive, and they’re one of the most important data types in the Swift language

Strings in Swift

Strings are powerful and intuitive, and they’re one of the most important data types in the Swift language. You use them to store and manipulate text, and as the basis for other data types, such as character arrays.

Swift strings are Unicode compliant, which means you can use them to store and manipulate text in any language. They’re also efficient, thanks to a number of optimizations built into the Swift language.

One of the most important features of strings is that they’re mutable, which means you can manipulate them after you create them. This makes them very versatile, and it allows programmers to easily manipulate and modify text.

Strings also support string interpolation, which allows programmers to insert values into them. This makes it easy to create strings that contain dynamic content, and it makes it possible to use string templates to create complex strings.

Overall, strings are an essential part of the Swift language and a powerful tool for storing and manipulating text.

How to Print a String in Swift

If you're new to programming, you may not realize that a string is just a sequence of characters. And printing a string simply means displaying it on the screen. In Swift, there are two ways to print a string:

Use the print() function:


    print("Hello, world!")

Use the println() function:


    println("Hello, world!")

Both functions do the same thing, but println() adds a new line character at the end of the string so that the next character printed will appear on a new line.

If you want to print multiple strings, you can separate them with commas:


    print("Hello,", "world!")

And that's all there is to printing strings in Swift!

String Interpolation in Swift

String interpolation is a powerful method that allows developers to insert values into strings. This can be useful when creating dynamic strings, such as when displaying user-generated content. Developers also use string interpolation for debugging purposes, as it allows them to see the values of variables and expressions at a glance.


    let s = "Hello, world!"
    print("s = \(s)") // s = Hello, world!
    
    let apples = 3
    let oranges = 5
    print("I have \(apples + oranges) pieces of fruit.") // I have 8 pieces of fruit.
    
    let height = 12.0
    let width = 10.0
    print("The rectangle's area is \(height * width).") // The rectangle's area is 120.0.
    
    let name = "Tim McGraw"
    let age = 25
    print("\(name) is \(age) years old.") // Tim McGraw is 25 years old.

How to Append Strings in Swift

When you append to a string, you add characters to the end of the string. For example, perhaps you want to add a period or exclamation point to the end of a string.

There are a few different ways to append strings in Swift. The most common way is to use the + operator. You can use this to append two strings together, creating a brand new string (i.e., string 3 = string 1 + string 2); or you can append one string to another to extend an existing string (i.e., string 2 = string 2 + string 3). For example:


    let str1 = "Hello"
    let str2 = " there"
    let str3 = str1 + str2 // "Hello there"
    let str4 = "Hello"
    let str5 = str4 + " there" // "Hello there"
    let str6 = "Hello" + " " + "there" // "Hello there"
You can call this method on any string, and it takes another string as a parameter

Using the append() Method

You can also append strings by using the append() method. You can call this method on any string, and it takes another string as a parameter. It appends the parameter string to the end of the string that it is called on. For example:


    // Append to string using append() method
    var str = "How are you?"
    str.append(" I am fine.")
    print(str) // How are you? I am fine.
    
    var val = "Hi! My name "
    val.append("is John.")
    print(val) // Hi! My name is John.

Using the += Operator

The += operator is one of the most commonly used operators in programming. It adds two values together and assigns the result to the first value.

For example, if we have a string variable str with the value "Hello", we can add "World" to it like this:


    str += "World"

This would assign the new value "HelloWorld" to the str variable.

You can use the += operator to append a string to the end of another string. This is similar to using the + operator but more concise. For example:


    // Append to string using += operator
    var str = "Hello, "
    str += "world!"
    print("str = \(str)") // Output: str = Hello, world!
    
    var val = "10"
    val += "0"
    print("val = \(val)") // val = 100

Appending a Multiline String to a String

Multiline string literals are enclosed in triple double quotes ("""). A multiline string literal can contain any characters, including line break characters.

In Swift, you can append a multiline string to another string by using the += operator. You can use this method when creating a long message or a block of HTML code.

To append a multiline string, simply use the += operator followed by the string you want to add. For example:


    // Appending multiline string to a string

    var str = "Hello, playground"
    str += """
    This is a multiline string
    """
    
    print(str) // Hello, playground
              // This is a multiline string
    
    var str1 = "1"
    var str2 = """
    This is a multiline string
    and it is appended to str1
    using the += operator
    """
    str1 += str2
    print(str1) // 1This is a multiline string
               // and it is appended to str1
               // using the += operator

Special Considerations With Unicode in Swift

When working with Unicode in Swift, you must keep in mind a few special considerations. First, Unicode uses variable-width encoding, which means that a single character can take up anywhere from one to four bytes. This can make Unicode string manipulation and comparison tricky because you need to take into account the different byte sizes.

Also, consider that Unicode defines a huge number of different characters — well over a million in total. This can make it difficult to find the character you're looking for, especially when working with non-Latin scripts.

Finally, you need to be aware of the different normalization forms defined by Unicode. These define how to canonicalize a string, and you can choose from four different forms. Depending on your use case, you may need to use a specific normalization form to ensure that your strings process correctly.


    // String manipulation with Unicode characters

    let uni4 = "João"
    let uni5 = "🐶"
    let uni6 = "João" + "🐶"
    let uni7 = "João" + "🐶" + "2"
    print(uni6) // João🐶
    print(uni7) // João🐶2

Conclusion

In this article, you learned about the concept of appending strings in Swift, which means adding an additional string to an existing string. The easiest way to accomplish this is to use the append function. You also saw examples of other ways to combine strings.

Now that you know how to append strings in your code, you should think about testing the code. You can use a platform like Waldo to perform mobile application testing. A mobile application testing platform allows you to test your code on a variety of devices, including mobile phones, tablets, and wearables. Thorough testing ensures that your code works properly on all devices and that users have a consistent experience across all devices.

Automated E2E tests for your mobile app

Waldo provides the best-in-class runtime for all your mobile testing needs.
Get true E2E testing in minutes, not months.

Reproduce, capture, and share bugs fast!

Waldo Sessions helps mobile teams reproduce bugs, while compiling detailed bug reports in real time.