How to case-insensitively check if a character exists in a string with Ruby

1 Answer

0 votes
# Case-insensitive check without a loop
def char_exists_ignore_case(s, target)
  # Convert both to lowercase and use include?
  s.downcase.include?(target.downcase)
end

# Define the string we want to search in
s = "RubyLanguage"

# Perform the case-insensitive check
exists = char_exists_ignore_case(s, "r")

# Print the raw boolean result
puts exists

# Conditional check
if exists
  puts "exists"
else
  puts "not exists"
end



=begin
run:

true
exists

=end

 



answered 3 hours ago by avibootz

Related questions

...