1

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

例如

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

5 回答 5

6

你可以看看这里,有很多例子。

例如,您可以通过

fun isLetters(string: String): Boolean {
    return string.all { it.isLetter() }
}
于 2020-09-28T12:47:02.223 回答
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)
于 2020-09-28T12:26:30.303 回答
1

@HakobHakobyan: 给出了一个检查 aString是否完全按字母顺序排列的好答案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是完全按字母顺序排列的。

于 2020-09-28T13:28:50.057 回答
0

如果您想构建任意查找(例如适合 base 64 编码的字符),您也可以执行以下操作:

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

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

val Char.isAcceptable get() = this in acceptable

"ab+5%".filter(Char::isAcceptable).let { print("VIPs: $it")}
>>>> VIPs: ab+
于 2020-09-29T00:14:45.123 回答
0

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

fun main(args: Array) {

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

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

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

于 2020-09-28T12:27:39.170 回答