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
+15k Golang : Get file permission
+18.3k Golang : Archive directory with tar and gzip
+6.3k Android Studio : How to detect camera, activate and capture example
+12.6k Javascript : Prompt confirmation before exit
+11.1k Golang : Display list of countries and ISO codes
+8k Golang : Serving HTTP and Websocket from different ports in a program example
+7k Golang : Qt splash screen with delay example
+5.9k Golang : Takes a plural word and makes it singular
+36.5k Golang : Comparing date or timestamp
+9.3k Golang : Allow Cross-Origin Resource Sharing request
+3.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+5.6k Golang : Totalize or add-up an array or slice example