title: Want to make left and right trim on Java String
body: <p>I have Java String which contain white space on both right and left side. I want to remove white space from both side.</p>
  
  <p>Code that I tried...</p>
  
  <pre><code>public class Test {
  
      public static void main(String args[])
      {
          String abc = "  Amebiasis  ";
          System.out.println(abc +" length "+abc.length());
          System.out.println(rtrim(abc)+" length "+rtrim(abc).length());
          System.out.println(ltrim(abc)+" length "+ltrim(abc).length());
  
          String ltrim = abc.replaceAll("^\\s+","");
          String rtrim = abc.replaceAll("\\s+$","");
  
          System.out.println("ltrim"+ltrim);
          System.out.println("rtrim"+rtrim);
      }
  
      public static String rtrim(String s) {
          int i = s.length()-1;
          while (i &gt;= 0 &amp;&amp; Character.isWhitespace(s.charAt(i))) {
              i--;
          }
          return s.substring(0,i+1);
      }
  
      public static String ltrim(String s) {
          int i = 0;
          while (i &lt; s.length() &amp;&amp; Character.isWhitespace(s.charAt(i))) {
              System.out.println("s.charAt(i)  "+s.charAt(i));
              i++;
          }
          return s.substring(i);
      }
  }
  </code></pre>
  
  <p>Output that I got...
   </p>
  
  <pre><code>  Amebiasis   length 13
    Amebiasis length 11
    Amebiasis   length 13
  ltrim  Amebiasis  
  rtrim  Amebiasis
  </code></pre>
  
  <p>Somehow it doesn't remove white space. What is wrong with my code, please help me on that.</p>