【问题标题】:In Kotlin, how to check if the input is alphabetic only在 Kotlin 中,如何检查输入是否仅为字母
【发布时间】:2020-09-28 12:16:03
【问题描述】:

在 kotlin 中,如何检查输入是否仅为字母。 输入可以是任何东西,StringIntDouble 等。

例如

val input = readLine()
if(check) {
   doSomeTask
}
else doSomethingElse

【问题讨论】:

  • 另外,根据您的要求,您可以随时使用"someString".isDigitsOnly().not(),但这不会排除明显包含数字的字符串。
  • 检查我的编辑。使用 toString() 将输入转换为字符串。它处理 null 并正确格式化数字。
  • 只有字母是否包含或排除标点符号和连字符等?
  • 只有字母...

标签: kotlin inout alphabetic


【解决方案1】:

你可以看看here,例子很多。

例如,您可以通过以下方式检查

fun isLetters(string: String): Boolean {
    return string.all { it.isLetter() }
}

【讨论】:

  • 非常感谢,我也发现 all{it.isLetter} 函数简单易读
【解决方案2】:

您可以使用带有字母范围的正则表达式:

fun alphabetCheck(input: String): Boolean {
    val regex = Regex("[a-zA-Z]+?")
    return regex.matches(input)
}

首先使用toString()将您的输入转换为字符串:

val str = input.toString()
val matchesAlphabet = alphabetCheck(str)

【讨论】:

  • 这是行不通的,无论输入什么,检查都会转移到 else 语句。
  • @AbhiAbhishek 抱歉,我错过了 +?在正则表达式模式的末尾。现在它应该可以工作了,我在 kotlin 操场上尝试过。这样可以确保它匹配所有输入,而不仅仅是第一个字符。
  • 您可以在正则表达式[a-zA-Z\\s\\.]+?中添加标点和空格等更多内容
【解决方案3】:

@HakobHakobyan 给出了一个检查String 是否完全按字母顺序排列的好答案:String.all { it.isLetter() }

我将借用他的解决方案来解决您问题的第二个方面,即

输入可以是任何东西,字符串、int 或 double 等。

这是另一个检查Any输入类型的方法:

fun isAplhabetical(input: Any): Boolean {
    when (input) {
        // if the input is a String, check all the Chars of it
        is String -> return input.all { it.isLetter() }
        // if input is a Char, just check that single Char
        is Char -> return input.isLetter()
        // otherwise, input doesn't contain any Char
        else -> return false
    }
}

它可以用在main()这样的例子中:

fun main() {
    val a = "Some non-numerical input"
    val b = "45"
    val c = "Some numbers, like 1, 2, 3, 4 and so on"
    val d: Int = 42
    val e: Double = 42.42
    val f: Float = 43.4333f
    val g = "This appears as entirely alphabetical" // but contains whitespaces
    val h = "ThisIsEntirelyAlphabetical"
    
    println("[$a] is" + (if (isAplhabetical(a)) "" else " not") + " (entirely) alphabetical")
    println("[$b] is" + (if (isAplhabetical(b)) "" else " not") + " (entirely) alphabetical")
    println("[$c] is" + (if (isAplhabetical(c)) "" else " not") + " (entirely) alphabetical")
    println("[$d] is" + (if (isAplhabetical(d)) "" else " not") + " (entirely) alphabetical")
    println("[$e] is" + (if (isAplhabetical(e)) "" else " not") + " (entirely) alphabetical")
    println("[$f] is" + (if (isAplhabetical(f)) "" else " not") + " (entirely) alphabetical")
    println("[$g] is" + (if (isAplhabetical(g)) "" else " not") + " (entirely) alphabetical")
    println("[$h] is" + (if (isAplhabetical(h)) "" else " not") + " (entirely) alphabetical")
}

输出是

[Some non-numerical input] is not (entirely) alphabetical
[45] is not (entirely) alphabetical
[Some numbers, like 1, 2, 3, 4 and so on] is not (entirely) alphabetical
[42] is not (entirely) alphabetical
[42.42] is not (entirely) alphabetical
[43.4333] is not (entirely) alphabetical
[This appears as entirely alphabetical] is not (entirely) alphabetical
[ThisIsEntirelyAlphabetical] is (entirely) alphabetical

只有最后一个 String 完全按字母顺序排列。

【讨论】:

  • 嗯...为什么String 没有在代码中突出显示?这是我的失败还是新语法突出显示之一?
  • 在他们多年未更新的移动应用上看起来不错。
【解决方案4】:

您可以检查字符的 ascii 值,如示例中所示:

fun main(args: Array) {

    val c = 'a'
    val ascii = c.toInt()

    println("The ASCII value of $c is: $ascii")
}

如果您查看ascii table,您会发现字母字符是介于值 65 和 90 之间的大写字母。对于小写字母,区间为 97 - 122。

【讨论】:

  • *) 用于非常有限的字母。
  • 你也有扩展的 ascii
【解决方案5】:

如果您想构建任意查找(比如适合 base 64 等编码的字符),您也可以这样做:

val acceptable = ('a'..'z').plus('A'..'Z').plus("+-/~".asIterable())

所以这是使用范围作为定义...字符范围的快速方法,并使用字符串轻松指定一些单独的字符(并将其转换为 Iterable<Char> 以便 plus 可以将它们添加到列表中.

val Char.isAcceptable get() = this in acceptable

"ab+5%".filter(Char::isAcceptable).let { print("VIPs: $it")}
>>>> VIPs: ab+

【讨论】:

    猜你喜欢
    • 2011-02-09
    • 1970-01-01
    • 2014-07-01
    • 2010-11-22
    • 1970-01-01
    • 2011-08-05
    • 2012-12-30
    • 2021-06-01
    • 1970-01-01
    相关资源
    最近更新 更多