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

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

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 Report

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 Report

0

       String arr[] = rt.split("to");
        //arr[0] is the 1st part and arr[1] second.
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!