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
+11.6k Android Studio : Password input and reveal password example
+20.8k Golang : Read directory content with filepath.Walk()
+3.7k JavaScript : Rounding number to decimal formats to display currency
+5.4k Android Studio : Hello World example
+6.8k Swift : Convert (cast) String to Double
+6.7k Golang : Reverse a string with unicode
+5k Golang : Extract unicode string from another unicode string example
+18.7k Golang : Read directory content with os.Open
+11.1k Golang : How to check if a string starts or ends with certain characters or words?
+11.4k Golang : HTTP response JSON encoded data
+5.6k Elasticsearch : Shutdown a local node
+7.2k Golang : How to check variable or object type during runtime?