可以使用正则表达式来判断输入是否为11位数字。以下是一个示例代码:

import scala.util.matching.Regex

def is11Digits(input: String): Boolean = {
  val pattern: Regex = """^\d{11}$""".r
  pattern.matches(input)
}

val input1 = "12345678901"
val input2 = "123456789012"

println(is11Digits(input1))  // 输出: true
println(is11Digits(input2))  // 输出: false

在上述代码中,我们使用了正则表达式 """^\d{11}$""" 来匹配11位数字。其中, ^ 表示字符串的开始, \d 表示数字, {11} 表示前面的元素重复11次, $ 表示字符串的结束。r 方法将字符串转换为 Regex 对象,然后使用 matches 方法来判断输入是否匹配该正则表达式。

scala判断输入是否为11位数字

原文地址: https://www.cveoy.top/t/topic/hNGK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录