Append or Concatenate Strings in Swift
To concatenate two strings in Swift, use concatenate operator +
. The syntax to concatenate two strings is:
var str = str1 + str2
where str1
and str2
are the two strings that we append. The concatenate operator +
returns the resulting string and we are storing the resulting concatenated string in str
variable.
Examples
In the following program, we have two strings in variables str1 and str2. These two strings are concatenated using Concatenation Operator +
.
main.swift
var str1 = "Hello" var str2 = " World!" var str = str1 + str2 print( str )
Output
Hello World!
Example 2 – Concatenate Two or More Strings in Swift
You can concatenate more than two strings in a single statement as shown in the below example.
main.swift
var str1 = "Swift" var str2 = "Tutorial" var str3 = "by" var str4 = "TutorialKart." //concatenate strings var str = str1 + " " + str2 + " " + str3 + " " + str4 print( str )
Output
Swift Tutorial by TutorialKart.
Conclusion
In this Swift Tutorial, we have concatenated two or more strings using concatenation operator +
.