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
+10.6k Nginx : TLS 1.2 support
+14.1k Golang : How to shuffle elements in array or slice?
+5.5k Swift : Get substring with rangeOfString() function example
+11.4k Golang : Convert(cast) float to int
+6.2k Grep : How to grep for strings inside binary data
+4.6k JQuery : Calling a function inside Jquery(document) block
+8.9k Golang : Create and shuffle deck of cards example
+77.9k Golang : How to return HTTP status code?
+24.9k Golang : convert rune to integer value
+4.8k Golang : micron to centimeter example
+6.9k Golang : Get Alexa ranking data example
+11.9k Golang : Split strings into command line arguments