忽略 Scala 中字符串的大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38267705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 08:27:41 来源:igfitidea点击:
Ignore case for a string in scala
提问by Nilesh
Consider:
考虑:
object HelloWorld {
def main(args: Array[String]): Unit = {
val s:String = "AbcD"
println(s.contains("ABCD"))
println(s.contains("AbcD"))
}
}
Output:
输出:
false
true
I need the result to be true in both cases regardless of the case. Is it possible?
无论情况如何,我都需要在这两种情况下结果都为真。是否可以?
回答by rapha?λ
If you really need containsuse
如果你真的需要contains使用
s.toLowerCase.contains("abcd")
But most likely you are looking for
但很可能你正在寻找
s.equalsIgnoreCase("abcd")
回答by Saqib Ali
with Regex
使用正则表达式
println(s.matches("(?i:.*" + "ABCD" + ".*)"))

