split and join
In this page:
split and join
String#split(separator) breaks a string into an array of substrings wherever the separator occurs, and Array#join(separator) does the reverse, combining array elements back into a single string. These are commonly paired when parsing and reconstructing delimited text like CSV data. Calling split with no arguments splits on any whitespace.
Note: text.split with no argument splits on any run of whitespace, which is handy for tokenizing plain sentences.
Example: split and join
csv_line = "apple,banana,cherry"
parts = csv_line.split(",")
puts parts.inspect
joined = parts.join(" | ")
puts joined
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: