Java – trim.() method does not work(java)

javastringtrim

So i'm trying to count the characters in a String without the spaces. But somehow my trim() method doesn't cut the spaces:

class Test {

HashMap<Character, Integer> testMap = new HashMap<>();

public void encode(String text) {
    String code = text.trim();
    for (int i = 0; i < code.length(); i++) {
        Character key = code.charAt(i);
        if (testMap.containsKey(key)) {
            testMap.put(key, testMap.get(key) + 1);
        } else {
            testMap.put(key, 1);
        }

    }
    System.out.println(testMap);
    System.out.println(code);
}

Main method:

public class MyMain {

public static void main(String[] args) {
    Test test = new Test();
    test.encode("Tessstttt faill");

}

}

So the output is:

{ =1, a=1, s=3, T=1, t=4, e=1, f=1, i=1, l=2}
Tessstttt faill

Where is my mistake. Thanks a lot ahead!!
(I'm not that experienced yet, so sorry in case of obvious mistake)

Best Solution

Trim won't remove all whitespace, only the leading and trailing. To remove all whitespace, you want something like this:

text.replaceAll("\\s","")