Back to questions
JuniorTheoryCommon
What does the program print?
let csv = "red,,blue"
let parts = csv.split(separator: ",")
print(parts.count, parts.joined(separator: "-"))2 red-blue
- Expecting
3 red--blue: thinkingsplitkeeps an empty piece between two separators. Swift'ssplitdrops empty pieces by default, sored,,bluegives two parts. - Expecting
2 red-blue-: thinkingjoinedadds the separator after every part. It puts the separator only between parts, so nothing follows the last one.
- How do you make
splitkeep the empty piece between the two commas? - What does the program print if
joinedgets the empty separator""?