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
+7.6k Golang : Convert(cast) io.Reader type to string
+9.5k Golang : Read file with ioutil
+6k Golang : Debug with Godebug
+13.1k Golang : Handle or parse date string with Z suffix(RFC3339) example
+13.9k Golang : Get current time
+20.1k Golang : How to get own program name during runtime ?
+7.3k Golang : Word limiter example
+29.9k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+14.4k Golang : Rename directory
+6.7k Golang : Get expvar(export variables) to work with multiplexer
+16.5k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+11.3k Golang : Delay or limit HTTP requests example