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.3k Golang : Generate Code128 barcode
+5.9k Golang : Count leading or ending zeros(any item of interest) example
+19.6k Golang : How to read integer value from standard input ?
+22.9k Golang : Detect (OS) Operating System
+8.1k Golang : Read until certain character to break for loop
+5.4k Golang : Find the length of big.Int variable example
+3.8k Unix/Linux : How to open tar.gz file ?
+7.5k Golang : How to profile or log time spend on execution?
+12.1k Golang : Execute terminal command to remote machine example
+17k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+3.8k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+9.3k Golang : Convert(cast) float to int