whitespace will not get removed

213 views Asked by At

Assume there is a url="www.example.com/". Using the following code I want to remove the trailing slash but it leaves a blank at the end of string (which by the way I do not know why) and using the rest of the code, I am trying to remove the white space but it will not work.

    String url="http://www.example.com/";
    int slash=url.lastIndexOf("/");


    StringBuilder myURL = new StringBuilder(url);

    if(url.endsWith("/")){
       myURL.setCharAt(slash, Character.MIN_VALUE );
       url=myURL.toString();
    }

    url=url.replaceAll("\\s+","");
    System.out.println(url);
8

There are 8 answers

0
René Link On BEST ANSWER

Because \s+ does not match Character.MIN_VALUE. Use ' ' instead.

String url="www.example.com/";
int slash=url.lastIndexOf("/");


StringBuilder myURL = new StringBuilder(url);

if(url.endsWith("/")){
   myURL.setCharAt(slash, ' ');
   url=myURL.toString();
}

url=url.replaceAll("\\s+","");
System.out.println(url);

But why don't you just remove the / ?

String url="www.example.com/";
int slash=url.lastIndexOf("/");

StringBuilder myURL = new StringBuilder(url);
myURL.deleteCharAt(slash);
System.out.println(myURL);
0
AmitB10 On
String url="www.example.com/";    
if(url.endsWith("/")){
            url = url.substring(0, url.length()-1);
        }

System.out.println(url);
0
Nail Samatov On

Instead of setCharAt() you should use deleteCharAt(). But the simplest way to do the job is

String url="www.example.com/";
url = url.substring(0, url.lastIndexOf("/"));
0
Arun Sudhakaran On

Replace you code inside your if-block with the below one

url = url.substring(0, url.length()-1).trim();

Then I hope you will no longer need that StringBuilder object also.

So your final code will look like

String url="www.example.com";
url = (url.endsWith("/")) ? url.substring(0, url.length()-1) : url;
System.out.print(url);
1
Valath On

Why are you complicating things, if this can be achieved in single line

String url="www.example.com/";
url=url.replace("/","");
System.out.println(url);
0
jaxfire On

The issue appears to be the use of the setCharAt method.

This method replaces a char with another char. So even though you have replaced it with the Character.MIN_VALUE which at first glance may appear to represent the literal Null it is actually still a unicode character ('\0000' aka the null character).

The simplest fix would be to replace...

myURL.setCharAt(slash, Character.MIN_VALUE );

with...

myURL.deleteCharAt(slash);

Further info regarding the null character...

Understanding the difference between null and '\u000' in Java

what's the default value of char?

This is my first answer so apologies if I've not kept to conventions.

3
Stefan On

Try to trim it: url = url.trim();

0
William Morrison On

I think the empty space is caused by Character.MIN_VALUE being interpreted as a space.

Try this. Its a little cleaner than your current replace code and will leave no space.

if(url.endsWith("/")){
    url = url.trim().substring(0, url.length-1);
}