ydmpcn
ydmpcn

Reputation: 587

Modifying string on java

I must turn String type modified xpathes to real xpathes by Java;

For example;

This kind of xpathes

_HTML_1__BODY_1__FORM_1__INPUT_3_

should turn to

/HTML[1]/BODY[1]/FORM[1]/INPUT[3]

I have no idea, Could you please help me

Upvotes: 1

Views: 321

Answers (2)

PeterK
PeterK

Reputation: 1723

Like lateralus said, Strings are immutable, so you can't change these.

However, having said that, you can use the replaceAll to return a modified version of the String, for example in this case:

String input = "_HTML_1__BODY_1__FORM_1__INPUT_3_";

String output = input.replaceAll("_(\\d+)_", "[$1]").replaceAll("_", "/");
// output = /HTML[1]/BODY[1]/FORM[1]/INPUT[3]

Edit

As for an explanation of the regex used in this case:

This method uses two separate regular expressions to return a modified String. Firstly, "_(\\d+)_", looks for numbers surrounded by two underscore characters _, \\d is a regex short hand for any digit. The surrounding brackets (...) capture the number, so that we can reference it in the replacement string.

When we make the first replacement, we replace with [$1], in here $1 refers back to the first captured group, i.e. the digit captured, and surrounds it in square brackets [...]. The underscores are also removed, as these were captured in the expression, if not in the group.

The second replaceAll call simply replaces all remaining underscore characters with a /.

Upvotes: 5

Michael Cornel
Michael Cornel

Reputation: 4004

Use a state machine:

 public static void main(String []args){
    String inp = "_HTML_1__BODY_1__FORM_1__INPUT_3_";

    String out = "";
    int state = 0;
    for (int i=0; i<inp.length(); i++) {
        if (inp.charAt(i) == '_') {
            switch (state) {
                case 0:
                    out += "/";
                    break;
                case 1:
                    out += "[";
                    break;
                case 2:
                    out += "]";
                    break;
                default:
                    throw new RuntimeException();
            }
            // Next state
            state = (state + 1) % 3;
        } else {
            out += inp.charAt(i);
        }
    }

    System.out.println(out);
    // Returns "/HTML[1]/BODY[1]/FORM[1]/INPUT[3]"
 }

Upvotes: 0

Related Questions