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"
Try this:
String[] output = "o to p".split(" to ");
String var1 = output[0]; // "o"
String var2 = output[1]; // "p"
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.
String arr[] = rt.split("to");
//arr[0] is the 1st part and arr[1] second.