String is a text with single-quote(' ') or double-quote(" ") .
'This is a string in single quote'
"This is a string in double quote"
Interpolation allows a string includes Ruby expression
Using:
"#{expression}"
Examples:
ruby = "Ruby Programming"
"Welcome to #{ruby}" #=> "Welcome to Ruby Programming"
"1 + 2 = {1 + 2}" #=> "1 + 2 = 3"
string_ruby = "Welcome to String in Ruby"
-
Get first character in string_ruby:
irb:001> string_ruby[0] => "W"
-
Get last character in string_ruby:
irb:001> string_ruby[-1] => "y"
-
Get second last character:
irb:001> string_ruby[-2] => "b"
-
Get first 7 characters:
irb:001> string_ruby[0..6] => "Welcome"
-
Get last 4 characters:
irb:001> string_ruby[-4..-1] => "Ruby"
-
Upcase a string
irb:001> "hello world".upcase => "HELLO WORLD"
-
Downcase a string
irb:001> "HELLO WORLD".downcase => "hello world"
-
Reverse characters in a string
irb:001> "hello world".reverse => "dlrow olleh"
-
Capitalize first character in a string
irb:001> "hello world".capitalize => "Hello world"
-
Get length of a string
irb:001> "hello world".length => 11
-
Replace a character in string by a new character
irb:001> "hello world".gsub("l", "L") => "heLLo worLd"