Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore case for a string in scala

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?

like image 733
Nilesh Avatar asked Jul 08 '16 13:07

Nilesh


2 Answers

If you really need contains use

s.toLowerCase.contains("abcd")

But most likely you are looking for

s.equalsIgnoreCase("abcd")
like image 154
raphaëλ Avatar answered Nov 15 '22 11:11

raphaëλ


with Regex

println(s.matches("(?i:.*" + "ABCD" + ".*)"))
like image 21
Saqib Ali Avatar answered Nov 15 '22 11:11

Saqib Ali