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

195
Vistas
Substring a middle characters in java

I want to substring the value "o to p" in to two string variables like this

o
p

I tried to substring this code but it doesn't seems to work.

String x = rt.substring(rt.indexOf(" to ")+1);

this returns "to p"

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Try this:

String[] output = "o to p".split(" to ");
String var1 = output[0]; // "o"
String var2 = output[1]; // "p"
over 4 years ago · Santiago Trujillo Denunciar

0

If you want to do it without split (which is easier, but anyway):

int pos = rt.indexOf(" to ");
String var1 = rt.substring(0, pos);
String var2 = rt.substring(pos + " to ".length());

The point you are missing is that indexOf returns the position of the start of the search string; if you want what comes after it, you need to increase the index by the length of that search string.

over 4 years ago · Santiago Trujillo Denunciar

0

       String arr[] = rt.split("to");
        //arr[0] is the 1st part and arr[1] second.
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
Recomiéndame algunas ofertas
Necesito ayuda