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
+4.5k Golang : Selection sort example
+8.5k Golang : Simple image viewer with Go-GTK
+8.2k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+5.7k Golang : Rename part of filename
+22.2k Golang : Change file read or write permission example
+71.8k Golang : How to convert character to ASCII and back
+18k Golang : Convert date string to variants of time.Time type examples
+5.9k Golang : Check from web if Go application is running or not
+8.7k Golang : Intercept and process UNIX signals example
+11.9k Golang : Get HTTP protocol version example
+5.8k Javascript : Put image into Chrome browser's console
+21k Golang : Strings to lowercase and uppercase example