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
+5.2k Golang : If else example and common mistake
+7.5k Golang : Reverse a string with unicode
+18.2k Golang : Get download file size
+47.6k Golang : How to convert JSON string to map and slice
+8k Golang : Oanda bot with Telegram and RSI example
+6.7k Golang : Pat multiplexer routing example
+11.9k Golang : md5 hash of a string
+16.5k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+11.5k Golang : convert(cast) float to string
+10.3k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+29.5k Golang : Get time.Duration in year, month, week or day
+11.5k Golang : GTK Input dialog box examples