title: Removing spaces at the end of a string in java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6652687/strip-leading-and-trailing-spaces-from-java-string">Strip Leading and Trailing Spaces From Java String</a>  </p>
  </blockquote>
  
  
  
  <p>When I import data to an application I need to get rid of the spaces at the end of certain strings but not those at the beginning, so I can't use trim()... I've set up a method:</p>
  
  <pre><code>public static String quitarEspaciosFinal(String cadena) {
      String[] trozos = cadena.split(" ");
      String ultimoTrozo = trozos[trozos.length-1];
      return cadena.substring(0,cadena.lastIndexOf(ultimoTrozo.charAt(ultimoTrozo.length()-1))+1);
      }
  </code></pre>
  
  <p>where cadena is the string I have to transform...</p>
  
  <p>So, if cadena = "  1234    " this method would return "  1234"...</p>
  
  <p>I'd like to know if there's a more efficient way to do this... </p>