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
+4.6k MariaDB/MySQL : How to get version information
+10.3k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+36.6k Golang : Display float in 2 decimal points and rounding up or down
+5.1k Golang : Convert lines of string into list for delete and insert operation
+12k Golang : Perform sanity checks on filename example
+23.7k Golang : Fix type interface{} has no field or no methods and type assertions example
+27.9k Golang : Move file to another directory
+16.5k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+8.7k Golang : Heap sort example
+24k Golang : Find biggest/largest number in array
+20.7k Golang : Underscore or snake_case to camel case example