To append a string to another string in Swift, you can use the +
operator or the append
method to concatenate the strings.
In this example,
str1
with a value of "Hello"
.+
operator to append the string " World"
to str1
.+
operator concatenates the two strings and returns the result.var str1 = "Hello"
str1 += " World"
print(str1)
Hello World
In this example,
str2
with a value of "banana"
.append
method to concatenate the string "apple"
to str2
.append
method appends the string to str2
.var str2 = "banana"
str2.append("apple")
print(str2)
bananaapple
In this tutorial, we learned How to Append a String to another String in Swift language with well detailed examples.