0

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);
user3049183
  • 136
  • 1
  • 3
  • 16

8 Answers8

4

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

Stefan
  • 1,590
  • 3
  • 18
  • 33
  • So, what the heck is the use of all those answers for removing whitespaces if a trim can fix it? – user3049183 Dec 16 '16 at 12:17
  • Your code will resolve this error, and is perfectly fine. Might I suggest also explaining to the OP why his code is leaving a whitespace in the first place? – William Morrison Dec 16 '16 at 12:18
  • @user3049183 trim will only remove whitespaces at the beginning or end of your string. In your case thats all that you need. But sometimes it might be necessary to remove all whitespaces, even those in the middle of a string. – OH GOD SPIDERS Dec 16 '16 at 12:21
2

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);
René Link
  • 48,224
  • 13
  • 108
  • 140
2
String url="www.example.com/";    
if(url.endsWith("/")){
            url = url.substring(0, url.length()-1);
        }

System.out.println(url);
AmitB10
  • 425
  • 6
  • 16
1

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("/"));
1

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.

Community
  • 1
  • 1
jaxfire
  • 156
  • 1
  • 7
0

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);
}
William Morrison
  • 10,953
  • 2
  • 31
  • 48
0

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);
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
0

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);
Valath
  • 880
  • 3
  • 13
  • 35
  • I had the same thought initially. Unfortunately this solution won't work in a general case for URLs. (Any URL with other slashes in it. – William Morrison Dec 16 '16 at 12:34