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
+52k Golang : How to get time in milliseconds?
+10.7k Golang : Bubble sort example
+26.6k Golang : Convert(cast) string to uint8 type and back to string
+10k Golang : ffmpeg with os/exec.Command() returns non-zero status
+4.8k Linux/MacOSX : How to symlink a file?
+31.9k Golang : How to convert(cast) string to IP address?
+8.3k Golang : HttpRouter multiplexer routing example
+14.9k Golang : Find commonalities in two slices or arrays example
+11.6k Use systeminfo to find out installed Windows Hotfix(s) or updates
+5.8k Golang : Frobnicate or tweaking a string example
+11.7k Golang : Simple file scaning and remove virus example
+8.2k Golang : Multiplexer with net/http and map