Ruby StringsUse strings: create literals, manipulate strings and use each char and each line.
String. A string contains text data. Strings are extensively used. In Ruby, we have powerful methods to transform, combine and test string data.
A string literal is string data directly specified in a program. In Ruby, we use the single-quote, or double-quote character to create string literals.String Literal
If you wish to use a for-loop over a string, this is possible in Ruby. But the each_char method is often a better choice.For, string
Replace. The sub() and gsub() methods can replace strings. To replace all matches in a string, we must use the gsub method. Sub() replaces only the first match.
There is no substring method on strings. But we can use ranges, indexes, and regular expressions to extract (and change) substrings.Substring
Often we need to store many strings together. We can concatenate them into a single string. But a string array is often a clearer, faster choice.String Arrays
These change letters in text. The ROT13 cipher, for example, shifts characters 13 places. It is easily reversed. We implement ROT13 in Ruby with the tr (translate) method.ROT13
A summary. String support in Ruby is complete and well-designed. Strings are common. They are often stored in collections like Arrays and Hashes.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.