我编写了以下代码来检查字符串是否包含特殊字符。代码对我来说看起来太复杂了,但我不知道如何让它更简单。有任何想法吗?
def containsNoSpecialChars(string: String): Boolean = {
val pattern = "^[a-zA-Z0-9]*$".r
return pattern.findAllIn(string).mkString.length == string.length
} //> containsNoSpecialChars: (string: String)Boolean
containsNoSpecialChars("bl!a ") //> res0: Boolean = false
containsNoSpecialChars("bla9") //> res1: Boolean = true