
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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
- Related Articles
- Swift program to convert string to an array
- Swift Program to Convert Set of String to Array of String
- Swift Program to Convert a String to Lowercase
- Swift Program to Convert a String to Uppercase
- Swift Program to convert the array of characters into the string
- Swift Program to convert the string into an array of characters
- Swift Program to Convert Array to Set
- How do I convert a Swift array to a string?
- Swift Program to Convert Set into Array
- Swift Program to convert int type variable to string
- Swift program to convert double type variable to string
- Swift program to convert boolean variable into string
- Swift Program to convert an array into a string and join elements with a specified character
- Java program to convert a character array to string
- Swift Program to Convert an Set of String to Comma Separated String
