Home
Map
join Example (Convert Array to String)Use the join method to convert a string array to a string. Specify a delimiter character.
Ruby
This page was last reviewed on Dec 9, 2022.
Join. Strings are everywhere in programs. And programs themselves are represented as strings. An array can contain strings—or it can be merged into a single string with delimiters.
Method notes. Join() acts on string arrays (or arrays of any type of element that are converted into strings). We can use join to round-trip our data with split().
String Array
An example program. Consider this program. We see a string array that contains three string literals. On the string array instance we call join.
And We specify a semicolon delimiter. The string values are merged into a single string, with a semicolon between each pair.
values = ["aa", "bb", "cc"] # Use the join method to combine a string array. joined = values.join(";") # Print the result. puts joined
aa;bb;cc
No delimiter. We can use join with no delimiter. This converts an array of strings (or 1-char strings which are like characters) into a string.
values = ["a", "b", "c"] # Join with no delimiter. # ... This turns an array of characters into a string. result = values.join puts result
abc
A review. With split() and join() we can round-trip our data processing. We have a string, and can split it apart. Then we modify elements, and join it together again.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.