我试图使用字符串替换另一个字符串,但没有发生。
String requestURI = "/webapps-ab/public/Test.jsp"
String contextName = "webapps-ab";
String newRequestURI = requestURI.replaceFirst(contextName,"webapps");
我期望 newRequestURI
为 "/webapps/public/Test.jsp"
。
最佳答案
您的 replace
调用应该是:
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");
使用:
String requestURI = "/webapps-ab/public/Test.jsp";
String contextName = "webapps-ab";
String newRequestURI = requestURI.replaceFirst(contextName, "webapps");
System.out.println("newRequestURI: " + newRequestURI);
输出将是您所期望的:
newRequestURI: /webapps/public/Test.jsp
关于java - 使用替换第一方法替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12287522/