Java - substring store into separate string variable

So I have a string like the following

"1: Command(1, 2, 3)"

I ultimately want to store 1 2 3 into String variables. How can I accomplish this?

The following is a snippet of code that reads an array of strings and attempts to print out the parameters of that string.

for (int i = 0; i < TableElements.length; i++) {
  if (sadTableElements[i] != null) {
    System.out.println("DEBUG " + sadTableElements[i]);
    Param0 = Integer.parseInt(TableElements[i].substring(TableElements[i].indexOf("(") + 1, 
                              TableElements[i].indexOf(",")));
    System.out.println(Param0);

    Param1 = Integer.parseInt(TableElements[i].substring(TableElements[i].indexOf(",") + 2, 
                              TableElements[i].indexOf(",")));
    System.out.println(Param1);

    Param2 = Integer.parseInt(TableElements[i].substring(TableElements[i].indexOf(",") + 2, 
                              TableElements[i].indexOf(")")));
    System.out.println(Param2);
  }
}

I get this output:

DEBUG 1: Command(1, 2, 3) 1 Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -2

It seems to stop at System.out.println(Param1);

Answers


indexOf() function in Java returns the first index of the matched substring so indexOf(",") will always return the index of the first "," that it finds in the entire input string.

i.e. indexOf(",") will always return the index of first "," found in the input string. To fetch the Param1 and Param2 , try using the below code instead :

    Param0 = Integer.parseInt(TableElements[i].substring(TableElements[i].indexOf("(") + 1, TableElements[i].indexOf(",")));
    System.out.println(Param0);
    Param1 = Integer.parseInt(TableElements[i].substring(TableElements[i].indexOf(",") + 2, TableElements[i].lastIndexOf(",")));
    System.out.println(Param1);
    Param2 = Integer.parseInt(TableElements[i].substring(TableElements[i].lastIndexOf(",") + 2, TableElements[i].indexOf(")")));
    System.out.println(Param2);

lastIndexOf() function returns the last index of the possible match i.e. it will return the index of second matched "," which should give the correct output.

Hope that helps!


 Param1 = Integer.parseInt(TableElements[i].substring(TableElements[i].indexOf(",") + 2, TableElements[i].indexOf(",")));

The TableElements[i].indexOf(",") gives you index of the first , character, which is lower than index got from TableElements[i].indexOf(",") + 2. That's why you are getting an exception.


Maybe better use regexp for this:

java.util.regex.Matcher m = java.util.regex.Pattern.compile("^\\d+: Command\\((\\d+), (\\d+), (\\d+)\\)").matcher("1: Command(1, 2, 3)");
if (!m.matches()) {
    throw new AssertionError("Your Regex Pattern or Input is bad");
}
System.out.println(m.group(1) + " | " + m.group(2) + " | " + m.group(3));

Output here is: 1 | 2 | 3

Note: If you adapt it, re-use the compiled pattern and do not recompile it for every input String


 Param1 = Integer.parseInt(TableElements[i].substring(TableElements[i].indexOf(",") + 2, TableElements[i].indexOf(",")));

Both indexOf(...) refer to the exact same colon. So you are building a substring going from index+2 to index. You could either use the split(...) function or save the created substring within a new string.


Need Your Help

What is the fastest way a client can communicate with a WCF service?

performance wcf

What is the fastest way a client can communicate with a WCF(Windows Communication Foundation) Service if both are hosted in the same private network?

How to keep button width in proportion to screen size using weights?

android xml layout android-linearlayout

I am creating an xml file. There I have two buttons on bottom of the screen. Both needs same width. So I done that by layout_weight(Check Fig 1. )