title: Java String.replace() - replaces more than just the substring I specify?
body: <p>As per <a href="http://codingbat.com/prob/p151940" rel="nofollow">this CodingBat problem</a> I am trying to do the following:</p>
  
  <blockquote>
    <p>Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged. </p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>public String withoutX(String str) {
      if (str.startsWith("x")) {
          str = str.replace(str.substring(0, 1), "");
      }
      if (str.endsWith("x")) {
          str = str.replace(str.substring(str.length()-1), "");
      }
      return str;
  }
  </code></pre>
  
  <p>This code replaces ALL the <code>x</code> characters in the string, rather than just the first and last. Why does this happen, and what would be a good way to solve it?</p>