Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!