1

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
deHaar
  • 17,687
  • 10
  • 38
  • 51
Abhishek
  • 59
  • 2
  • 12

5 Answers5

7

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() }
}
Hakob Hakobyan
  • 1,111
  • 8
  • 15
2

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)
P Fuster
  • 2,224
  • 1
  • 20
  • 30
  • This isn't working, whatever the input is given, the check is moving to else statement. – Abhishek Sep 28 '20 at 12:58
  • @AbhiAbhishek sorry I was missing the +? at the end of the Regex pattern. Now it should work I tried in the kotlin playground. This ensures it matches all the input and not just the first character. – P Fuster Sep 28 '20 at 15:18
  • You can add more things like punctuation and spaces to the regex expression `[a-zA-Z\\s\\.]+?` – P Fuster Sep 28 '20 at 15:20
2

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.

deHaar
  • 17,687
  • 10
  • 38
  • 51
0

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.

Andrea Nisticò
  • 466
  • 2
  • 4
  • 11
0

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+
cactustictacs
  • 17,935
  • 2
  • 14
  • 25