TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to RUBY

Ruby Join Example (Convert Array to String)

Use the join method to convert a string array to a string. Specify a delimiter character.
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 Arrays
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.

Ruby program that uses join values = ["keyboard", "monitor", "CPU"] # Use the join method to combine a string array. joined = values.join(";") puts joined Output keyboard;monitor;CPU
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.
Ruby program that uses no delimiter values = ["a", "b", "c"] # Join with no delimiter. # ... This turns an array of characters into a string. result = values.join puts result Output 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.Split
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf