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
+21.3k Golang : Create and resolve(read) symbolic links
+14.3k Golang : Get uploaded file name or access uploaded files
+18.5k Golang : Write file with io.WriteString
+5.6k Fix fatal error: evacuation not done in time problem
+9.5k Golang : Apply Histogram Equalization to color images
+14.6k How to automatically restart your crashed Golang server
+7.3k Golang : How to iterate a slice without using for loop?
+27.4k Golang : Convert CSV data to JSON format and save to file
+13.7k Android Studio : Password input and reveal password example
+8k Golang : Trim everything onward after a word
+7.6k Android Studio : AlertDialog to get user attention example