Question

I have a string

String origStr = "/unwanted/wanted1/wanted2/and_so_on"

i want to make this as

String finalStr = "wanted1/wanted2/and_so_on"

I thought of doing that using regEx in Java. But struck on this.

Was it helpful?

Solution

origStr.replaceFirst("/[^/]+/", "");

should work

OTHER TIPS

You can do something like this -

    String str = "/unwanted/wanted1/wanted2/and_so_on";
    String temp = str.substring(str.indexOf("/", 1)+1);
    System.out.println(temp);

Without using regEx.You can use split("/") method to make it into String array.And cutting down the first element.

String [] arr= origSTr.split("/");

Use this arr as you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top