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
+32.4k Golang : Call a function after some delay(time.Sleep and Tick)
+4.4k Unix/Linux : How to archive and compress entire directory ?
+32.2k Golang : Create x509 certificate, private and public keys
+10.3k Golang : How to flush a channel before the end of program?
+6.4k Golang : Lock executable to a specific machine with unique hash of the machine
+6.8k Golang : Get today's weekday name and calculate target day distance example
+28.8k Golang : Remove characters from string example
+3.7k JavaScript : Rounding number to decimal formats to display currency
+5.9k Golang : Normalize email to prevent multiple signups example
+9.8k Golang : Removes punctuation or defined delimiter from the user's input