The split(separator:)
method returns the longest possible sub-sequences of a collection. In this case, we want to use this method to split a string into an array of characters.
This is the syntax for the split(separator:)
method:
string.split(separator: separatorElement)
separatorElement
: This is the element that should be split upon.
The split
method returns smaller sub-parts of the array that are obtained from the subject string.
// create some stringslet numbers = "1 2 3 4"let alphabets = "a-b-c-d"// split strings into characterslet numberCharacters = numbers.split(separator: " ")let alphabetCharacters = alphabets.split(separator: "-")// print resultsprint(numberCharacters)print(alphabetCharacters)
numbers
and strings
.