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 Setting $GOPATH environment variable for Unix/Linux and Windows
+8.7k Golang : Build and compile multiple source files
+9.1k Golang : Timeout example
+13.3k Golang : Get user input until a command or receive a word to stop
+9.3k Golang : Read file with ioutil
+18k Golang : How to remove certain lines from a file
+8.2k Golang : How to check if input string is a word?
+31.5k Golang : Convert an image file to []byte
+13.2k Golang : Count number of runes in string
+8k Prevent Write failed: Broken pipe problem during ssh session with screen command
+5.8k Golang : Experimenting with the Rejang script
+16.5k Golang : Get the IPv4 and IPv6 addresses for a specific network interface