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

224
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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