Java String.split() and compare

1.2k views Asked by At

Im working with flying saucer and want to export an xhtml to an pdf. Everything works fine, but now I want to add an empty column, for example for descriptions or something.

I want to create a method addColumn(). which should add in every row of the table at the end a new, empty cell.

I tried following code:

            String[] arr = content.split("<td");
            String test = "";
            for (int i = 0; i < arr.length; i++) {
                if(i != 0){
                arr[i] = "<td" + arr[i];
                test += arr[i];
                }
            }

This should split the content on every beginning "td" tag. String.split("<td") removes the "<td" from the content so i want to add it again.

But if i compare those:

if(test.equalsIgnoreCase(content)){
     System.out.println("SUCCESS");
}
else{
     System.out.println("FAIL");
}

I always fail.

Just help me to get the right content back out of the array, this would make me go a step in the right direction!

Thank you.

1

There are 1 answers

0
Chriki On BEST ANSWER

Try to replace your split line with this:

String[] arr = content.split("<td", -1);

Otherwise you will loose some input in arr, see the split(String) API doc:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The added -1 makes sure that your content can also contain "<tr" at its beginning, for example. See the split(String, int) API doc for further explanations.