Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

282
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Asistente virtual