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
+5.8k Golang : Warp text string by number of characters or runes example
+9.3k Golang : Convert file unix timestamp to UTC time example
+7.5k PHP : How to parse ElasticSearch JSON ?
+15.5k Golang : How to implement two-factor authentication?
+4.9k Get website traffic ranking with Similar Web or Alexa
+10.4k Golang : How to flush a channel before the end of program?
+13.8k Golang : How to check if IP address is in range
+5.7k Get Facebook friends working in same company
+13.6k Golang : Normalize unicode strings for comparison purpose
+13k Golang : Get uploaded file name or access uploaded files
+24.6k Golang : How to write CSV data to file