I'm trying to break up a string 192.168.1.2:6060;rport;branch=z9hG4bKNskFdtGO4"
.
I wanted to extract the port:ip
before the first semicolon, and the branch number after the equal sign.
The code I tried was
temp = in.next();
System.out.println(temp.contains(";"));
System.out.println(temp.contains("="));
System.out.println("Temp: " + temp + " ");
sipName = temp.substring(0, temp.charAt(';'));
branch = temp.substring(temp.charAt('='));
I added the printlns to show if they characters were at least being found in the strings.
When I ran the code I get an StringIndexOutOfBoundsError at line sipName = temp.substring(0, temp.charAt(';'));
My console output is:
true
true
Temp: 192.168.1.2:6060;rport;branch=z9hG4bKb8NGxwdoR
Exception in thread "Thread-1" java.lang.StringIndexOutOfBoundsException: String index out of range: 59
...
It fails even if I just try System.out.println(temp.charAt(';'));
I'm not sure why this is happening. Is anyone able to explain? I'm stumped.