Concatenate array of strings into a single array in Swift

This tutorial is based on the topic to concatenate arrays of strings into a single array. We can do this task very easily by joining the two given arrays of strings. This is gonna be so easy and interesting task, our task is to join two arrays of strings. You might have been provided with the arrays of strings or might need to create one. I have discussed it from the creation itself. We will create two arrays of the strings of your choice and then we will concatenate them.

Starting with creating the first array of Strings-

var array1:[String] = ["Concatenating","Two Strings"]

Let’s create the second array of Strings-

var array2:[String] = ["in","Swift"]

So we have created two arrays of strings now its time to join or concatenate them both-

var new_array = array1 + array2

Simply + operator in Swift would concatenate the two arrays of strings. Let’s try to print the output-

print(new_array)

The complete code-

var array1:[String] = ["Concatenating","Two Strings"]
 
var array2:[String] = ["in","Swift"]
 
var new_array = array1 + array2
 
print(new_array)
Output:-
["Concatenating","Two Strings","in","Swift"]

Here we have completed the task to join two strings in Swift and we have done it using the easiest method though there are some other possible methods too. I hope this might have helped you.

KEEP LEARNING! KEEP CODING!

Leave a Reply

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