var numbers = "Hello,Goodbye,Hi,Bye" var numbersArr = numbers.componentsSeparatedByString(",")
//["Hello"."Goodbye","Hi","Bye"]
Above is a basic representation of what I'm trying to do. I'm trying to use componentsSeparatedByString()
to split a string with commas into an array, where each of the components of the array are between each of the commas from the original strings.
I am using IBM Swift Sandbox (Sorry, I'm on windows :) ), and in Swift 3.0, I am getting this error message:
value of type 'String' has no member 'componentsSeparatedByString'
I know Swift 3 is rather new, and is that is why I couldn't find ANY other references for this error.
Swift String split() The split() method breaks up a string at the specified separator and returns an array of strings.
To split a String by Character separator in Swift, use String. split() function. Call split() function on the String and pass the separator (character) as argument. split() function returns a String Array with the splits as elements.
To split a string to an array in Swift by a character, use the String. split(separator:) function. However, this requires that the separator is a singular character, not a string.
You can also split a string by a substring, using NString's componentsSeparatedByString method. You should be able to use NSString's "componentsSeparatedByCharactersInSet:" to split on multiple characters.
It looks like there is a components(separatedBy:)
on String
:
import Foundation let words = "apple binary cat delta echo".components(separatedBy: " ") print(words)
IBM Playground link: http://swiftlang.ng.bluemix.net/#/repl/57868332b4e4e9971bf9f4e8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With