HomeSearch

Ruby Strings

Use 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.
String method list. In Ruby we call methods with operators or method syntax. With an exclamation mark, we can modify a string variable in-place. This can reduce code in programs.centerchompchopcountdeletedowncaseeach_chareach_linegsubincludeindexinsertlengthljustreverserindexrjustsplitstripsubupcase<< (Append)+ (Concat)* (Multiply)
Literals. 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
For-loop, length. 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.
Substring. There is no substring method on strings. But we can use ranges, indexes, and regular expressions to extract (and change) substrings.Substring
String arrays. 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
Ciphers. 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.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.