Swift program to convert string to an array


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

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 string into an array we can use any of the following method.

Below is a demonstration of the same −

Input

Suppose our given input is −

Value = “true”

Output

The desired output would be −

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

Method 1 - Using components(separatedBy:) Function

To convert string into an array we can use in-build components(separatedBy:) method. This method divide the string into substrings with the help of specified string separator. Or in other words, this function return an array of substrings of the given string separated by the given separator.

Syntax

Following is the syntax −

mystr. components(separatedBy:sep)

Here sep parameter is used to separate the substring of the given string. Its value can be a character or a string.

Example

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

import Foundation
import Glibc

// String 1
var myStr1 : String = "Today is the nice day for fishing"
print("Original String 1: ", myStr1)

// Array
var myArr1 = myStr1.components(separatedBy: " ")
print("Array of myStr1:", myArr1)

// String 2
var myStr2 : String = "I-Like-ChamCham"
print("Original String 2: ", myStr2)

// Array
var myArr2 = myStr2.components(separatedBy: "-")
print("Array of myStr2:", myArr2)

Output

Original String 1:  Today is the nice day for fishing
Array of myStr1: ["Today", "is", "the", "nice", "day", "for", "fishing"]

Original String 2:  I-Like-ChamCham
Array of myStr2: ["I", "Like", "ChamCham"]

Here, in the above code, we have two different strings −

var myStr1 : String = "Today is the nice day for fishing"
var myStr2 : String = "I-Like-ChamCham"

Now we convert them into array using components(separatedBy:) method −

var myArr1 = myStr1.components(separatedBy: " “)
var myArr2 = myStr2.components(separatedBy: "-")

So we get the following array −

Array of myStr1: ["Today", "is", "the", "nice", "day", "for", "fishing"]
Array of myStr2: ["I", "Like", "ChamCham"]

Method 2 - Using Array() Initialiser

We can also convert a string into an array using Array() initialiser. It splits the given string into an array of individual characters.

Syntax

Following is the syntax −

Array(Value)

Example

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

import Foundation
import Glibc

// String 1
var myStr1 : String = "Today is-nice"
print("Original String 1: ", myStr1)

// Array
var myArr1 = Array(myStr1)
print("Array of myStr1:", myArr1)

// String 2
var myStr2 : String = "ChamCham"
print("Original String 2: ", myStr2)

// Array
var myArr2 = Array(myStr2)
print("Array of myStr2:", myArr2)

Output

Original String 1:  Today is-nice
Array of myStr1: ["T", "o", "d", "a", "y", " ", "i", "s", "-", "n", "i", "c", "e"]

Original String 2:  ChamCham
Array of myStr2: ["C", "h", "a", "m", "C", "h", "a", “m"]

Here in the above code convert two strings into array using Array() initialiser and we get the following output −

Array of myStr1: ["T", "o", "d", "a", "y", " ", "i", "s", "-", "n", "i", "c", "e"]
Array of myStr2: ["C", "h", "a", "m", "C", "h", "a", “m"]

Here, you can see that Array() convert all the content into any array if character including letters, space, special characters.

Method 3 - Using map() Function

We can also convert a string into an array using map() function. It iterate each character in the string. Or we can say that this method return an array which contain the result of mapping the specified closer over the sequence’s item.

Syntax

Following is the syntax −

String.map{String($0)}

Here map () is used to iterate each character in the string and $0 is used to convert each character into string.

Example

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

import Foundation
import Glibc

// String 1
var myStr1 : String = "Today is-nice"
print("Original String 1: ", myStr1)

// Array
var myArr1 = myStr1.map{String($0)}
print("Array of myStr1:", myArr1)

// String 2
var myStr2 : String = "ChamCham"
print("Original String 2: ", myStr2)

// Array
var myArr2 = myStr2.map{String($0)}
print("Array of myStr2:", myArr2)

Output

Original String 1:  Today is-nice
Array of myStr1: ["T", "o", "d", "a", "y", " ", "i", "s", "-", "n", "i", "c", "e"]

Original String 2:  ChamCham
Array of myStr2: ["C", "h", "a", "m", "C", "h", "a", “m"]

Method 4 - Using for Loop

We can also convert a string into an array with the help of for loop. Here we use for loop to iterate through each character of the string and then using append() function we add the character into the array.

Example

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

import Foundation
import Glibc

// String 1
var myStr : String = "Today is-nice"
print("Original String:", myStr)

// Array
var myArr = [Character]()

// Converting string into array using for loop
for c in myStr{
   myArr.append(c)
}

print("Array:", myArr)

Output

Original String: Today is-nice
Array: ["T", "o", "d", "a", "y", " ", "i", "s", "-", "n", "i", "c", "e"]

Updated on: 13-Dec-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements