1

I have a URL string where I need to replace the last collection of characters after the final "/"

In Ruby, this would be:

str = "/some/url/structure"
ar = str.split("/")
ar[ar.length-1] = "path"
string = ar.join("/")
>> "/some/url/path"

How can I do this in Java?

6 Answers 6

7

This snippet demonstrates how to do it with Java:

System.out.println("one/two/four".replaceAll("/[^/]+$", "/three"));

It takes the input String ("one/two/four") and replaces everything starting from the last slash (including the slash) with the replacement String.

Minor change - + instead of * leaves urls with a slash as a last char unchanged. Seems a more practical solution to me.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I'm accepting this as it is the most concise, and marking the rest as useful as they all arrive at the correct answer
What is i have "one/three/two/four/two/three" as input string and i want to replace the last "/three" with "mystring" ?...replaceAll() will not replace all occurences of old string to the new string ?
@Kartrik - it the snippet fulfills the requirement. Try it yourself: ideone.com/sXvpY (Great - received the first downvote for a working solution ;-) )
sorry sorry, by mistake i downvoted your answer. I was actually downvoting last 2nd answer.
thanks its working yaar..what i thought was it would replace all occurences of old string with new string.. You are Great..
5

I don't fully understand, what exactly do you want, but you may construct a StringBuilder from your string, use it's lastIndexOf("/") method to determine last / position, and, finally, use append() or replace() to add something to the end (or instead of the current ending).

Comments

2

Or use this if you are not so used to regular expressions.

url.substring(0, url.lastIndexOf("/")+1).concat("newvalue");

Comments

2

Frozen Spider is absolutely right. You can use the following Code :

String str = "/some/url/structure";
int lst=str.lastIndexOf("/");
str=str.replace(str.substring(lst+1),"path");
System.out.println(str);

What i did is, i took last index of '\', and replaced the string starting from last '/', with "path".

In Fully Shortest Form:

System.out.println(str.replace(str.substring(str.lastIndexOf("/")+1),"path"));

Comments

0

Try This...

String str = "/some/url/structure";
    String[] ar = str.split("/");
    int n = ar.length;
    str = "";
    for (int i = 0; i < (n - 1); i++) {
        str += ar[i] + "/";
    }
    str += "path";
    System.out.println(str);

Comments

-1

I think you want to replace /structure with your content. Then you should try str.replce('/structure','/parth').

1 Comment

That would also replace /structure inside the string "/foo/structure/bar" (ie. not the last occurrence)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.