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
+23.8k Golang : Use regular expression to validate domain name
+5.2k PHP : See installed compiled-in-modules
+7.5k Golang : Detect sample rate, channels or latency with PortAudio
+4.3k Golang : Valued expressions and functions example
+28.1k Golang : Connect to database (MySQL/MariaDB) server
+4.7k JavaScript: Add marker function on Google Map
+5.7k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+8k Golang : Handle Palindrome string with case sensitivity and unicode
+5.5k PHP : Convert CSV to JSON with YQL example
+9.4k Golang : Get all countries currencies code in JSON format
+11k Golang : Fix go.exe is not compatible with the version of Windows you're running
+36.6k Golang : Display float in 2 decimal points and rounding up or down