Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
How to detect the last string of a TextView contains 0 to 9

I'm a beginner in Android software development. I'm making a calculator app to test my skills. In a calculator there are 0-9 numeric and some others operators like +, -, *, / etc. In the time of Equal functionality, I have to make sure that the last string of the TextView has any number (0-9), not any operators.

My equal fun is like that:

fun onEqual(view: View) {
    if (tv_input.text.contains("0-9")) {
        Toast.makeText(this, "Last string is a number, not operator", Toast.LENGTH_SHORT).show()
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You need to use Kotlin regular expression matches

val lastString = "573" // input
val pattern = "-?\\d+(\\.\\d+)?".toRegex()
/**
-?         allows zero or more - for negative numbers in the string.
\\d+       checks the string must have at least 1 or more numbers (\\d).
(\\.\\d+)? allows zero or more of the given pattern (\\.\\d+) 
           In which \\. checks if the string contains . (decimal points) or not
           If yes, it should be followed by at least one or more number \\d+.
**/
val isLastString = pattern.matches(lastString)
over 4 years ago · Santiago Trujillo Relatório

0

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/ends-with.html

fun String.endsWith(
    suffix: String,
    ignoreCase: Boolean = false
): Boolean

Returns true if this string ends with the specified suffix.

over 4 years ago · Santiago Trujillo Relatório

0

Thanks everyone. I did my own solution.

Here is the code I've wrote:

fun onEqual(view: View) {
    if (!tv_input.text.toString().equals("")) {
        val last = tv_input.text.toString()
        val lastNumber: String = last.get(last.length - 1).toString()
        Toast.makeText(this, lastNumber, Toast.LENGTH_SHORT).show()
    }else {
        return
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda