Here:In this example, we use the percent "%" character at the start of two literals.
And:In this form, we can use another character, such as a vertical bar or "+" as a delimiter. We can avoid escaping double-quotes.
Based on: Ruby 2 Ruby program that uses string literals # String literals. value1 = %|This is "ruby" string| value2 = %+This is also 'one'+ value3 = "This is another \"string\"" # Display results. puts value1 puts value2 puts value3 Output This is "ruby" string This is also 'one' This is another "string"
Tip:You must assign to the result of upcase() and downcase(). The original string instance is not modified.
Instead:A modified copy of the string is made. This is returned. We assign to this reference.
Ruby program that uses upcase, downcase # An input string. value = "Dot Net Perls" # Change cases. upper = value.upcase() lower = value.downcase() # Display results. puts upper puts lower Output DOT NET PERLS dot net perls
Here:We add two string variables, value1 and value2, along with the "/" literal. We display the result.
Ruby program that concatenates strings # Two string values. value1 = "Ruby" value2 = "Python" # Concatenate the string values. value3 = value1 + "/" + value2; # Display the result. puts value3 Output Ruby/Python
Multiplication:This program also multiplies a string. This concatenates the same string several times. It changes "1" to "111" here.
Ruby program that computes string length # An input string. test = "1" # Multiply the string by 3. test2 = test * 3 # Display string and its length. puts test2 puts test2.length Output 111 3
Next:On the next iteration, it is assigned to the next char. In the example, "c" is the current char.
Tip:As with other "each" iterators, this reduces the possibility of errors when looping in programs.Iterators
Ruby program that uses each_char value = "ruby" # Loop over each character with each_char. value.each_char do |c| # Write char. puts c end Output r u b y
Important:If no newlines occur in the string, only one string will be returned by each_line.
Ruby program that uses each_line # String literal with two newline characters. data = "Ruby\nPython\nPerl" # Loop over lines with each_line. data.each_line do |line| # Write line. puts line end Output Ruby Python Perl
Here:We see that the string "plato" contains the string "to". It does not contain the string "not".
And:The "include?" method is case-sensitive. This means the string "plato" does not include the string "PLA".
Ruby that uses include method value = "plato" # String includes "to". if value.include? "to" puts "1" end # String does not include "not". if !value.include? "not" puts "2" end # String does not include "PLA" uppercase. if !value.include? "PLA" puts "3" end Output 1 2 3
Invalid:With an invalid index, the insert() method will throw an IndexError. We may need to check against the string's length.
Negative:With a negative index, the insertion is based on the last index. If we pass -1, the insertion occurs before the last character.
Ruby that uses insert # Input string. value = "socrates" # Insert string into the input string. value.insert(3, "k-") puts value # Now prepend two characters. value.insert(0, "??") puts value Output sock-rates ??sock-rates
Tip:You can specify a padding character. The default character is a space, but we can use any character.
Warning:If you call center() on a string that is too long to be centered in that size, the method will do nothing.
Ruby that uses center # This string has 8 chars. value = "prytanes" # Center with spaces to size of 10. a = value.center(10) puts "[" + a + "]" # Center with stars to size of 13. b = value.center(13, "*") puts b Output [ prytanes ] **prytanes***
In-place:The "chomp!" method modifies a string in-place. So we don't need to assign to the result of "chomp!" with a string variable.
Chop:This method is similar to chomp, but less safe. It removes the final character from the input. This can lead to corrupt data.
Ruby that uses chomp # Chomp removes ending whitespaces and returns a copy. value = "egypt\r\n" value2 = value.chomp puts value2 # Chomp! modifies the string in-place. value3 = "england\r\n" value3.chomp! puts value3 # An argument specifies a part to be removed. value4 = "european" value4.chomp! "an" puts value4 Output egypt england europe
Tip:The append operator changes the value of the string. So after we append once, the actual string data is changed.
Tip 2:If you want to retain the original data, you can copy a string by assigning another string reference to it.
Ruby that uses append value = "cat" puts value # Append this string (surrounded by spaces). value << " in " puts value # Append another string. value << "the hat" puts value Output cat cat in cat in the hat
Range:We can use a range of characters within the argument to count: "a-c" means "abc."
Ruby that uses count method value = "Plutarch" # The letter "a" occurs once. a = value.count "a" puts a # The letters "a" and "r" occur twice in total. b = value.count "ar" puts b # Letters in range "a" through "c" occur twice in total. c = value.count "a-c" puts c Output 1 2 2
Tip:If more than two characters are passed to crypt, only the first two characters are used.
Tip 2:The first two characters of the salt string appear at the start of the encrypted string.
Deterministic:With the same salt argument, crypt() is deterministic. So one use for it is storing the result of crypt for a string.
Ruby that uses crypt # Crypt this string with salt string "aa". value = "ruby" result = value.crypt "aa" puts result # Crypt another value. value = "sapphire" result = value.crypt "99" puts result Output aauZSSiXB7FbU 99MByjDtoc6Tc
Sort:A string's letters cannot be directly sorted. But we can convert the string to an array, and sort that.Sort, String
Ruby that uses reverse value = "rat" # Reverse in-place. # ... Without an exclamation, reverse returns a new string. value.reverse! puts value Output tar