The correct syntax for use them is explain below.
Double quote "" : Use when you need string interpolation and character escapes.
Single quote '' : Use when is not needed perform actions inside the strings,all what you see is what you get.
Back quote `` : Use for communicate with the operative system by commands.
# single quote
puts '2 + 2 = 4'
# double quote
puts "2 + 2 = #{2+2}"
# back quote
puts `which ruby`
puts '2 + 2 = 4'
# double quote
puts "2 + 2 = #{2+2}"
# back quote
puts `which ruby`
Element reference
Get a substring of an specific string using the index or position of the characters.
# Element reference
sentence = ' The Goths were a powerful tribe '
puts(sentence[4])
# Result is 'G'
# white space are count
# using range
puts(sentence[4..8])
# begin from 4 to 8, using two dots
puts(sentence[4...8])
# start from 4 to 8, using three dots, is equal to 4 to 8 less 1.
puts(sentence[1..-10])
# negative numbers start count from right to left
# Element reference
sentence = ' The Goths were a powerful tribe '
puts(sentence[4])
# Result is 'G'
# white space are count
# using range
puts(sentence[4..8])
# begin from 4 to 8, using two dots
puts(sentence[4...8])
# start from 4 to 8, using three dots, is equal to 4 to 8 less 1.
puts(sentence[1..-10])
# negative numbers start count from right to left
# Element reference
String concatenations
In Ruby use "+" operand to concatenate strings
# simple
puts 'Hello' + 'World'
puts 'Hello' 'World'
# Let's use a variable
puts 'Hello' + 'World'
puts 'Hello' 'World'
# Let's use a variable
exp = 'Hello'
puts exp + 'World'
# Let's use two variables
exp 1= 'Hello'
exp2= 'World'
puts exp1+ exp2
puts exp + 'World'
# Let's use two variables
exp 1= 'Hello'
exp2= 'World'
puts exp1
#Using <<
puts 'Hello' << 'World'
Using some methods of the String class
Ruby allow get a list of the different available methods for ruby classes.
# set a string
exp = "a b c d e f g h l"
puts exp.methods
exp = "a b c d e f g h l"
puts exp.methods
Checking if a string has an specific word or character.
# set a string
exp = "a b c d e f g h l"
puts exp.include?('h')
# the output will be a boolean type, in this case true
exp = "a b c d e f g h l"
puts exp.include?('h')
# the output will be a boolean type, in this case true
Changing strings to uppercase and lowercase.
# change to uppercase
exp = ' a b c d e f g '
puts exp.upcase
# change to lowercase
exp = ' A B C D E F G '
puts exp.downcase!
puts exp.downcase!
# Capitalize (the first letter)
exp = ' a b c d e f g '
puts exp.capitalize!
# Convert from uppercase to lowercase or from lowercase to uppercase
exp = 'A b C d E f '
puts exp.swapcase
exp = ' a b c d e f g '
puts exp.upcase
# change to lowercase
exp = ' A B C D E F G '
puts exp.downcase!
puts exp.downcase!
# Capitalize (the first letter)
exp = ' a b c d e f g '
puts exp.capitalize!
# Convert from uppercase to lowercase or from lowercase to uppercase
exp = 'A b C d E f '
puts exp.swapcase
String Literals
%q/ Single quoted string /
%Q/ Double quoted string /
%Q/ Double quoted string /
%/ Double quoted string /
# what you see is what you get
%w/ #{2+3} 5 7 /
# return the result of 2+3
%W/ #{2+3} 5 7 /
%r| regular expression |
%s/ return a :symbol /
%x/ command for the OS /
# what you see is what you get
%w/ #{2+3} 5 7 /
# return the result of 2+3
%W/ #{2+3} 5 7 /
%r| regular expression |
%s/ return a :symbol /
%x/ command for the OS /
Thus conclude the explanation of handling string in Ruby.
puts ' Thanks for reading! '
With 💜 from Santo Domingo, DR.