我http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName= No Technicians in Area在基于Struts的Web应用程序中有一个链接.
URL中的变量在justificationName其vales之前有一些空格,如图所示.当我获得justificationName 使用request.getParameter("justificationName")它的价值时,我会在URL中给出带有空格的值.我想删除这些空格.我试过trim()尝试,str = str.replace(" ", "");但他们中的任何一个都没有删除那些空格.任何人都可以通过其他方式来消除空间.
注意到我做了一件事,右键单击链接并打开链接到新标签,我注意到链接看起来像.
http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName=%A0%A0%A0%A0%A0%A0%A0%A0No%20Technicians%20in%20Area
Run Code Online (Sandbox Code Playgroud)
值得注意的是,它在地址栏中显示的%A0是空白区域,并且还显示%20空间以及链接,并告诉区别,如果有人知道它.
编辑 这是我的代码
String justificationCode = "";
if (request.getParameter("justificationName") != null) {
justificationCode = request.getParameter("justificationName");
}
justificationCode = justificationCode.replace(" ", "");
Run Code Online (Sandbox Code Playgroud)
注意:replace函数从字符串内部删除空格但不删除起始空格.例如,如果我的字符串在使用替换后为"This is string",则变为"Thisisstring"
提前致谢
字符串在Java中是不可变的,因此该方法不会更改您传递的字符串但会返回一个新字符串.您必须使用返回的值:
str = str.replace(" ", "");
Run Code Online (Sandbox Code Playgroud)