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
+14.6k Golang : How to check for empty array string or string?
+33.4k Golang : How to check if slice or array is empty?
+11.1k Golang : How to flush a channel before the end of program?
+12.1k Golang : 2 dimensional array example
+21.1k Curl usage examples with Golang
+8.4k Golang : Set or add headers for many or different handlers
+16.1k Golang :Trim white spaces from a string
+13.6k Golang : Get dimension(width and height) of image file
+9.6k Golang : ffmpeg with os/exec.Command() returns non-zero status
+18.2k Golang : Aligning strings to right, left and center with fill example
+6.6k How to let Facebook Login button redirect to a particular URL ?
+6.3k Golang : How to determine if request or crawl is from Google robots