Swift Program to convert an array into a string and join elements with a specified character


In this article, we will learn how to write a swift program to convert an array into a string and join elements with a specified character.

An array is used to store elements of same data type in an order. Here we use the following methods to join elements of the array with a specified character −

  • Using joined(separator:) function

  • Without using inbuilt function

Method 1: Using joined(separator:) Function

So to convert an array into a string and join elements with a specified character we use joined(separator:) method. This method is used to concatenate the elements of the given array or sequence and insert the given separator in between each element.

Syntax

func joined(separator:sep)

Here, separator parameter contains a character or a sequence to insert in between the each element of the given sequence.

Algorithm

  • Step 1 − Create an array of string type.

  • Step 2 − Declare another variable to store the character which will insert in-between the each elements of the array.

  • Step 3 − Join the element of the given array using joined(separator:) function.

  • Step 4 − Print the output.

Example

Following Swift program to convert an array into a string and join elements with a specified character.

import Foundation
import Glibc

// Creating an array of string type
let array = ["Sky", "rain", "Cloudy"]

// Character which going to insert in between two elements
let sep = "-"

// Joining the elements of the given array with a 
// specified character
let resString = array.joined(separator: sep)
print("Resultant String is:",resString)

Output

Resultant String is: Sky-rain-Cloudy

Here in the above code, we have an array of string type. Then create a variable ‘sep’ to store the character(that is “-“) which will further insert in between the elements of the given array. Then use joined(separator:) method to join the elements of the given array along with the separator and print the result on the output screen.

Method 2: Without Using in-built Function

In this method, we use for-in loop with enumerated() function to convert an array into a string and join elements with a specified character. Here, enumerated() function return a pair(index, element), where index represent the consecutive integer striating from zero and element represent the element of the given array or sequence.

Syntax

func enumerated()

Here, this function does not take any parameter.

Algorithm

  • Step 1 − Create an array of string type.

  • Step 2 − Declare another variable to store the character which will insert in-between the each elements of the array.

  • Step 3 − Create an empty string to store the resultant string.

  • Step 4 − Run a for-in loop with enumerated() function to find the index value and the corresponding element.

  • Step 5 − Add the element in the resString.

  • Step 6 − Check if the given index is less than array.count-1, then add the specified character in the resString.

  • Step 7 − Print the output.

Example

Following Swift program to convert an array into a string and join elements with a specified character.

import Foundation
import Glibc

// Creating an array of string type
let array = ["My", "Car", "Fly", "From", "The", "Bridge"]

// Character which going to insert in between two elements
let separator = "*"

var resString = ""

for (index, ele) in array.enumerated() {
   resString += ele
   if index < array.count - 1 {
      resString += separator
   }
}

print("Resultant String is:", resString)

Output

Resultant String is: My*Car*Fly*From*The*Bridge

Here in the above code, we have an array of string type. Then create a variable ‘separator’ to store the character(that is “*“) which will further insert in between the elements of the given array. Then run a for-in loop along with enumerated() function find the index and the corresponding element. Then add the element in the resString. Now check if the index is less than the array.count-1, then concatenate the senator with the resString. This process continue till the end of the array and print the output.

Conclusion

So this is how we can convert an array into a string and join elements with a specified character using inbuilt or without using built function.

Updated on: 19-Jul-2023

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements