Swift program to convert array to a string


This tutorial will discuss how to write swift program to convert array to a string.

String is an ordered collection of characters. For example: “TutorialsPoint”, “Pinky”, etc. To create a string type variable we use String keyword.

var str : String

Array is a collection of similar data types. Like any array of integer can contain only integer values, it does not accept string values.

var arr = [Int]

To convert an array to a string we can use any of the following method.

Below is a demonstration of the same −

Input

Suppose our given input is −

Array = [“t”, “r”, “u”, “e”]

Output

The desired output would be −

String = “true”

Method 1- Using joined(separator:) function

To convert an array to a string we can use in-built joined() function. This function is used to join elements of the specified sequence and insert the specified separator in between them. This method works perfectly fine with array of string and characters but it you want to convert an array of integer into string, then you cannot call this function directly, if you do so then you will get error. Instead you convert the array of number into array of string and then use joined() function

Syntax

Following is the syntax −

StringName.joined(separator:)

Example 1

The following program shows how to convert an array into a string.

import Foundation
import Glibc

// Array of characters
var myArr = ["M", "A", "N", "G", "O"]

// Creating string from array
// Here the characters of string is separated by ","
var mystr1 = myArr.joined(separator: ",")

print("Array:", myArr)
print("String 1:", mystr1)

// Creating string from array
// With no separator
var mystr2 = myArr.joined()

print("String 2:", mystr2)

Output

Array: ["M", "A", "N", "G", "O"]
String 1: M,A,N,G,O
String 2: MANGO

Example 2

The following program show about Creating string from an array of numbers

import Foundation
import Glibc

// Array of integer
var myArr = [34, 56, 78, 34, 56, 23]

// Creating string from array
// with separator
var mystr1 = myArr.map { String($0) }.joined(separator: "-")

print("Array:", myArr)
print("String 1:", mystr1)

// Creating string from array
// With no separator
var mystr2 = myArr.map { String($0) }.joined()

print("String 2:", mystr2)

Output

Array: [34, 56, 78, 34, 56, 23]
String 1: 34-56-78-34-56-23
String 2: 345678345623

Here, in the above code, we have an array of integer so to convert array into string, we first convert the array of integer into array of string using map() function and them use joined() function −

var mystr1 = myArr.map { String($0) }.joined(separator: “-")

Here, map () is used to iterate each character in the string and $0 is used to convert each character into string and then we apply joined() function to create a string.

Example

The following program shows Creating a string from an array of string

import Foundation
import Glibc

// Array of string
var myArr = ["I", "Like", "Blue", "Sky"]

// Creating string from array
// with separator
var mystr1 = myArr.joined(separator: "-")

print("Array:", myArr)
print("String 1:", mystr1)

// Creating string from array
// With no separator
var mystr2 = myArr.joined()

print("String 2:", mystr2)

Output

Array: ["I", "Like", "Blue", "Sky"]
String 1: I-Like-Blue-Sky
String 2: ILikeBlueSky

Method 2 - Using reduce() function

We can also convert an array to a string using in-built reduce() function. This function convert the given array of string into a string. Or we can say that this function is used to concatenate the elements of the given sequence according to the specified closure.

Syntax

Following is the syntax −

array.reduce(initalVal, nextVal)

Here initialVal is the first value from which the closure execute. Whereas nextVal is the closure. Closure also takes two parameters: stater value and the current value in the sequence, and the operation is applied between these two values and it continue till the end of the collection.

Example

The following program shows how to convert an array into a string.

import Foundation
import Glibc

// Array of string
var myArr = ["I", "Like", "Blue", "Sky"]

// Creating string from array
var mystr1 = myArr.reduce("", +)

print("Array:", myArr)
print("String:", mystr1)

Output

Array: ["I", "Like", "Blue", "Sky"]
String: ILikeBlueSky

Updated on: 13-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements