Swift : Convert string array to array example
Problem :
You have a string array and you want to convert it to array. How to do that?
Solution :
First, remove the [
and ]
from the string with stringByTrimmingCharactersInSet()
function. Then, split it to an array by commas and finally remove the white spaces.
For example:
let array = [1,2,3,4];
let stringArray = array.description;
let result = stringArray
.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "[]"))
.componentsSeparatedByString(",")
.map { return $0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) }
Hope this helps!
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+19.1k Golang : Clearing slice
+8.9k Golang : On lambda, anonymous, inline functions and function literals
+12.4k Golang : 2 dimensional array example
+11k Golang : Get UDP client IP address and differentiate clients by port number
+8k Javascript : How to check a browser's Do Not Track status?
+6k Golang : Shuffle array of list
+17k Golang : How to generate QR codes?
+14.9k Golang : Find commonalities in two slices or arrays example
+17.1k Golang : XML to JSON example
+14.6k Golang : Overwrite previous output with count down timer
+8.4k Golang : Convert word to its plural form example
+21.2k Golang : For loop continue,break and range