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
+9.7k Golang : Simple image viewer with Go-GTK
+7.8k Golang : Capture text return from exec function example
+9.5k Swift : Convert (cast) String to Integer
+5.3k Golang : Extract sub-strings
+31.3k Golang : Smarter Error Handling with strings.Contains()
+9.1k Golang : Wait and sync.WaitGroup example
+23.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+12.9k Golang : Get uploaded file name or access uploaded files
+3.9k Unix/Linux : How to pipe/save output of a command to file?
+18.3k Golang : Count JSON objects and convert to slice/array
+21.2k Golang : Round float to precision example
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax