@@ -0,0 +1,1051 @@
+package others.algorithm;%0A%0Aimport org.junit.jupiter.api.Assertions;%0Aimport org.junit.jupiter.api.Test;%0A%0A/**%0A */%0Apublic class TrimAllSpace %7B%0A%0A    @Test%0A    void test() %7B%0A        Assertions.assertEquals(%22%22, trimAll(%22   %22));%0A        Assertions.assertEquals(%22abc%22, trimAll(%22   abc%22));%0A        Assertions.assertEquals(%22abc%22, trimAll(%22abc   %22));%0A        Assertions.assertEquals(%22abc%22, trimAll(%22   abc   %22));%0A        Assertions.assertEquals(%22abc def NY.%22, trimAll(%22   abc  def    NY.  %22));%0A    %7D%0A%0A    private String trimAll(String str) %7B%0A        StringBuilder ret = new StringBuilder();%0A        boolean inSpace = false;%0A        for (int i = 0; i %3C str.length(); i++) %7B%0A            if (str.charAt(i) == ' ') %7B%0A                if (ret.length() == 0) continue; // leading space%0A                inSpace = true;%0A            %7D else %7B%0A                if (inSpace) %7B%0A                    ret.append(%22 %22);%0A                    inSpace = false;%0A                %7D%0A                ret.append(str.charAt(i));%0A            %7D%0A        %7D%0A        return ret.toString();%0A    %7D%0A%0A%7D%0A

trim all space