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
+8k Golang : How to tell if a file is compressed either gzip or zip ?
+6.9k Golang : Chunk split or divide a string into smaller chunk example
+6.7k Golang : Encrypt and decrypt data with x509 crypto
+4k Golang : Convert(cast) []byte to io.Reader type
+4.9k Golang : Replace a parameter's value inside a configuration file example
+2.8k Golang : Getting Echo framework StartAutoTLS to work
+3.5k Android Studio : Checkbox for user to select options example
+1.4k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+9.6k Golang : Convert date string to variants of time.Time type examples
+4.4k Golang : Natural string sorting example
+2.7k Golang : How to stop user from directly running an executable file?
+6.8k Golang : Find out mime type from bytes in buffer