-4

i have a link http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName= No Technicians in Area in my struts based web application.

The variable in URL justificationName have some spaces before its vales as shown. when i get value of justificationName using request.getParameter("justificationName") it gives me that value with spaces as given in the URL. i want to remove those spaces. i tried trim() i tries str = str.replace(" ", ""); but any of them did not removed those spaces. can any one tell some other way to remove the space.

Noted one more thing that i did right click on the link and opened the link into new tab there i noticed that link looks like.

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

Notable point is that in the address bar it shows %A0 for white spaces and also show %20 for space as well see the link and tell the difference please if any one have idea about it.

EDIT Here is my code

String justificationCode = "";
        if (request.getParameter("justificationName") != null) {            
            justificationCode = request.getParameter("justificationName");
        }
        justificationCode = justificationCode.replace(" ", "");

Note: replace function remove the space from inside the string but not removing starting spaces. e-g if my string is " This is string" after using replace it becomes " Thisisstring"

Thanks in advance

NoNaMe
  • 6,020
  • 30
  • 82
  • 110

6 Answers6

5

Strings are immutable in Java, so the method doesn't change the string you pass but returns a new one. You must use the returned value :

str = str.replace(" ", "");
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Well. It should. This really removes all standard white space. Your problem might be elsewhere, you should debug step by step to see what happens (or print the string before and after replacement). – Denys Séguret May 21 '13 at 13:21
  • trim only remove the spaces from the extremities. You'd use it as str = str.trim(); – Denys Séguret May 21 '13 at 13:25
3

Manual trim

You need to remove the spaces the string. This will remove any number of consecutive spaces.

String trimmed = str.replaceAll(" +", "");

If you want to replace all whitespace characters:

String trimmed = str.replaceAll("\\s+", "");

URL Encoding

You could also use an URLEncoder, which sounds like a more appropriate way to go:

import java.net.UrlEncoder;
String url = "http://localhost:8080/reporting/" + URLEncoder.encode("pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName= No Technicians in Area", "ISO-8859-1");
flavian
  • 28,161
  • 11
  • 65
  • 105
1

You have to assign the result of the replace(String regex, String replacement) operation to another variable. See the Javadoc for the replace(String regex, String replacement) method. It returns a brand new String object and this is because the String(s) in Java are immutable. In your case, you can simply do the following

String noSpacesString = str.replace("\\s+", "");
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
0

You can use replaceAll("\\s","") It will remove all white space.

commit
  • 4,777
  • 15
  • 43
  • 70
0

If you are trying to remove the trailing and ending white spaces, then

s = s.trim();

Or if you want to remove all the spaces the use :

s = s.replace(" ","");

Community
  • 1
  • 1
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

There are two ways of doing one is regular expression based or your own way of implementing the logic

replaceAll("\\s","")

or

    if (text.contains(" ") || text.contains("\t") || text.contains("\r") 
       || text.contains("\n"))   
    {  
   //code goes here
    }
flavian
  • 28,161
  • 11
  • 65
  • 105