title: How to get leading and trailing spaces in String java
body: <p>In all other related posts people asked about removing leading or/and trailing spaces in String java. Now, my question is how to get the leading or trailing spaces? What I could think is for example to get the trailing spaces using such a function:</p>
  
  <pre><code>private String getTrailingSpaces(String str) {
       return str.replace(str.replaceFirst("\\s+$", ""), "");
  }
  </code></pre>
  
  <p>But I'm not sure if this is correct or even if it is, is there any better way to do this?</p>