Swift – Append String
To append a string to another string in Swift, use the String method String.append()
.
A simple code snippet to demonstrate how to use String.append() method
</>
Copy
str1.append(str2)
where str1
and str2
are strings, and append()
method appends str2
to str1
. str1
is modified while str2
remains unchanged.
Example
In the following program, we have two strings in variables str1
and str2
. We append str2 to str1 using String.append() method.
main.swift
</>
Copy
var str1 = "Hello"
var str2 = " World!"
str1.append(str2)
print( str1 )
Output
Hello World!
Conclusion
In this Swift Tutorial, we learned how to append a string to another string using String.append() method.