I have a string variable with content:
varMessage =
\"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\\n\"
\"/my/name/is/balaji.so\\n\"
You can also do this...
my_string = "Hello world"
if my_string["Hello"]
puts 'It has "Hello"'
else
puts 'No "Hello" found'
end
# => 'It has "Hello"'
This example uses Ruby's String #[] method.
Ternary way
my_string.include?('ahr') ? (puts 'String includes ahr') : (puts 'String does not include ahr')
OR
puts (my_string.include?('ahr') ? 'String includes ahr' : 'String not includes ahr')
user_input = gets.chomp
user_input.downcase!
if user_input.include?('substring')
# Do something
end
This will help you check if the string contains substring or not
puts "Enter a string"
user_input = gets.chomp # Ex: Tommy
user_input.downcase! # tommy
if user_input.include?('s')
puts "Found"
else
puts "Not found"
end