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.4k Golang : Verify Linux user password again before executing a program example
+5.5k Golang : Spell checking with ispell example
+16k Golang : When to use init() function?
+5.3k Golang : Break string into a slice of characters example
+3.6k Linux/MacOSX : Search and delete files by extension
+9.4k Golang : Simple File Server
+7.8k Golang : HTTP Routing with Goji example
+22.8k Golang : Call function from another package
+3.9k Google : Block or disable caching of your website content
+6.8k Golang : Regular Expression find string example
+7.1k Golang : Emulate NumPy way of creating matrix example
+8.8k Golang : Translate language with language package example