How to check if a character exists in a string with Ruby

1 Answer

0 votes
# Define the string we want to search in
s = "rubylang"

# Check for a character using include?
has_r = s.include?('r')   # true
has_z = s.include?('z')   # false

# Print the raw boolean results
puts has_r
puts has_z

# Conditional check
if s.include?('r')
  puts "exists"
else
  puts "not exists"
end



=begin
run:

true
false
exists

=end

 



answered 10 hours ago by avibootz
...