Scala 字符串matches()方法及示例
matches() 方法用于检查所述字符串是否与参数中指定的正则表达式相匹配。
方法定义:Boolean matches(String regex)
返回类型。如果字符串与正则表达式匹配,则返回真,否则返回假。
例子 #1 :
// Scala program of int matches()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying matches method
val result = "Preeti".matches(".*i")
// Displays output
println(result)
}
}
输出。
true
所以,这里的正则表达式与所述的字符串匹配。所以,它返回真。
例子#2
// Scala program of int matches()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying matches method
val result = "Preeti".matches(".i.*")
// Displays output
println(result)
}
}
输出。
false
极客教程