简体   繁体   中英

In Kotlin, how to check if the input is alphabetic only

In kotlin, how to check if the input is alphabetic only. Input could be anything, a String , Int or Double etc.

For example

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

You can have a look here , there are a lot of examples.

for example you can check via

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

You can use a regex with the alphabet range:

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

First convert your input to string by using toString() :

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

A good answer for checking if a String is entirely alphabetical was given by @HakobHakobyan: String.all { it.isLetter() } .

I will borrow his solution to target a second aspect of your question, that is

Input could be anything, a string, int or double etc.

Here's another method that checks Any input type:

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
    }
}

and it can be used in an example main() like this:

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")
}

The output is

[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

Only the last String is entirely alphabetical.

You can check the ascii value of a character as in the example:

fun main(args: Array) {

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

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

If you look at the ascii table , you can see that alphabetic characters are the one between the values 65 and 90 for capital letters. For small letters you have the interval 97 - 122.

If you want to build an arbitrary lookup (say characters that fit an encoding like base 64) you can do this kind of thing too:

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

So that's using ranges as a quick way of defining a... range of characters, and using a string to easily specify some individual ones (and turning it into an Iterable<Char> so plus can add them to the list.

val Char.isAcceptable get() = this in acceptable

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM