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

229
Vistas
Split text in Java

input text:

inptext = "inp1(A, Var1), inp1(B,Var1)"

expected output:

optext = "inp1(A, Var1)", "inp1(B,Var1)"

Code:

String [] splitText = inptext.split(", ");
for (String obj:splitText )
{
   System.out.println(obj);
}

Current output:

inp1(A
Var1)
.
.

Interpreting the current output:

optext = "inp1(A", "Var1)"

Please suggest me on correcting this.

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

0

You can use Regex lookahead to achieve this, e.g.:

String inptext = "inp1(A, Var1), inp1(B,Var1)";
String[] tokens = inptext.split("(?<=\\)),\\s");
for(String token : tokens){
    System.out.println(token);
}
over 4 years ago · Santiago Trujillo Denunciar

0

if you have space after the comma in the input "like inp1" you can use this:

String inptext = "inp1(A, Var1), inp1(B,Var1)";
        String[] tmp = inptext.split("\\), ");
        for (String a : tmp) {
            if (!a.substring(a.length()-1).equals(")"))
                a += ")";
            System.out.println(a);

        }
over 4 years ago · Santiago Trujillo Denunciar

0

you could combine String#substring and String#indexOf to retrieve the required data.

String inptext = "inp1(A, Var1), inp1(B,Var1)";
String firstResult = inptext.substring(0,inptext.lastIndexOf(", "));
String secondResult = inptext.substring(inptext.lastIndexOf(", ")).replace(", ","");
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