0

I was trying to use the string replace with another, it doesn't happen.

String requestURI = "/webapps-ab/public/Test.jsp"
String contextName = "webapps-ab";
String newRequestURI = requestURI.replaceFirst(contextName,"webapps");

I am expecting newRequestURI to be "/webapps/public/Test.jsp".

2 Answers 2

3

Your replace call should be:

String newRequestURI = requestURI.replaceFirst(contextName, "webapps");

Using:

String requestURI = "/webapps-ab/public/Test.jsp";
String contextName = "webapps-ab";
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");
System.out.println("newRequestURI: " + newRequestURI);

The output will be what you're expecting:

newRequestURI: /webapps/public/Test.jsp

ideone example.

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

4 Comments

I tried the same piece of code but still see the old request URI. Does '/' make any difference.
@1078081 you must have typos remaining. See the link to the ideone version producing the expected output
I am running the code exactly as pb2q has posted it and am seeing the correct output. Could you please post what the exact code is you're running?
It it working now. In my real code, I didn't notice that there are some upper case letters. Thank you
0

When referencing a variable, do not surround it in quotes, as that converts it into a literal String object.

String newRequestURI = requestURI.replaceFirst(contextName, "webapps");

Comments

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.