Terry Li
Terry Li

Reputation: 17268

Java String trim has no effect

Java String trim is not removing a whitespace character for me.

String rank = (some method);
System.out.println("(" + rank + ")");

The output is (1 ). Notice the space to the right of the 1.

I have to remove the trailing space from the string rank but neither rank.trim() nor rank.replace(" ","") removes it.

The string rank just remains the same either way.

Edit: Full Code::

Document doc = Jsoup.connect("http://www.4icu.org/ca/").timeout(1000000).get();
Element table = doc.select("table").get(7);
Elements rows = table.select("tr");
for (Element row: rows) {
  String rank = row.select("span").first().text().trim();
  System.out.println("("+rank+")");
}

Why can't I remove that space?

Upvotes: 22

Views: 50769

Answers (7)

Ertuğrul Çetin
Ertuğrul Çetin

Reputation: 5231

I had same problem and did little manipulation on java's trim() method.
You can use this code to trim:

public static String trimAdvanced(String value) {

        Objects.requireNonNull(value);

        int strLength = value.length();
        int len = value.length();
        int st = 0;
        char[] val = value.toCharArray();

        if (strLength == 0) {
            return "";
        }

        while ((st < len) && (val[st] <= ' ') || (val[st] == '\u00A0')) {
            st++;
            if (st == strLength) {
                break;
            }
        }
        while ((st < len) && (val[len - 1] <= ' ') || (val[len - 1] == '\u00A0')) {
            len--;
            if (len == 0) {
                break;
            }
        }


        return (st > len) ? "" : ((st > 0) || (len < strLength)) ? value.substring(st, len) : value;
    }

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691973

The character is a non-breaking space, and is thus not removed by the trim() method. Iterate through the characters and print the int value of each one, to know which character you must replace by an empty string to get what you want.

Upvotes: 4

Baz
Baz

Reputation: 36894

The source code of that website shows the special html character &nbsp;. Try searching or replacing the following in your java String: \u00A0.

That's a non-breakable space. See: I have a string with "\u00a0", and I need to replace it with "" str_replace fails

rank = rank.replaceAll("\u00A0", "");

should work. Maybe add a double \\ instead of the \.

Upvotes: 68

heretolearn
heretolearn

Reputation: 6555

Since String in java are immutable ie they cannot be changed. You need to reassign it to some temporary string. And then using that string you can convert it into int.

String temp=rank.trim()
int te= Integer.parseInt(temp)

Upvotes: 0

Kishor Sharma
Kishor Sharma

Reputation: 599

Trim function returns a new copy of the string, with leading and trailing whitespace omitted.

rank = rank.trim();// This will remove and save rank without leading and trailing spaces

will give the result you want.

Replace method will not work if you pass empty string for replacement.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726829

You should assign the result of trim back to the String variable. Otherwise it is not going to work, because strings in Java are immutable.

String orig = "    quick brown fox    ";
String trimmed = original.trim();

Upvotes: 7

Julius
Julius

Reputation: 2864

Are you assigning the String?

String rank = " blabla "; 
rank = rank.trim();

Don't forget the second assignment, or your trimmed string will go nowhere.

You can look this sort of stuff up in the API as well: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()

As you can see this method returns a String, like most methods that operate on a String do. They return the modified String and leave the original String in tact.

Upvotes: 2

Related Questions