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
+46.8k Golang : Marshal and unmarshal json.RawMessage struct example
+20.2k Golang : How to run your code only once with sync.Once object
+3.7k Java : Get FX sentiment from website example
+14.6k Golang : How to determine if user agent is a mobile device example
+10k Random number generation with crypto/rand in Go
+19.2k Golang : Clearing slice
+18.9k Golang : convert int to string
+21.5k Golang : How to force compile or remove object files first before rebuild?
+6.2k Golang : Dealing with backquote
+6.1k Linux/MacOSX : Search for files by filename and extension with find command
+9.1k Golang : Populate or initialize struct with values example
+6.4k Golang : How to get capacity of a slice or array?