首页 > 解决方案 > 使用 .include 忽略字符串中的大小写?

问题描述

在这个程序中:

thisstring = "sample text"

if thisstring.include? "sample TEXT"
  puts "heres some output"
end

有没有办法可以在这里使用 casecmp 来忽略 TEXT 中的大写字母?

标签: ruby

解决方案


那这个呢?

if thisstring.downcase.include?("sample TEXT".downcase)
  puts "heres some output"
end

或者:

if thisstring.match?(/#{sample TEXT}/i)
  puts "heres some output"
end

推荐阅读