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
+14.1k Golang : Validate hostname
+11k Golang : Encrypt and decrypt data with x509 crypto
+6.8k Golang : Error reading timestamp with GORM or SQL driver
+9.8k Golang : Web routing/multiplex example
+22.9k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+22.9k Golang : Call function from another package
+37.9k Golang : How to iterate over a []string(array)
+22.8k Golang : Daemonizing a simple web server process example
+13.5k Golang : Save(pipe) HTTP response into a file
+4.5k Golang : Frobnicate or tweaking a string example
+6.4k Golang : Example of how to detect which type of script a word belongs to
+18.3k Golang : Compare floating-point numbers