11

Possible Duplicate:
Strip Leading and Trailing Spaces From Java String

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:

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);
    }

where cadena is the string I have to transform...

So, if cadena = " 1234 " this method would return " 1234"...

I'd like to know if there's a more efficient way to do this...

Community
  • 1
  • 1
diminuta
  • 1,545
  • 8
  • 32
  • 55

5 Answers5

30

You can use replaceAll() method on the String, with the regex \s+$ :

return cadena.replaceAll("\\s+$", "");

If you only want to remove real spaces (not tabulations nor new lines), replace \\s by a space in the regex.

kgautron
  • 7,915
  • 9
  • 39
  • 60
10
    String s = "   this has spaces at the beginning and at the end      ";
    String result = s.replaceAll("\\s+$", "");
m0skit0
  • 25,268
  • 11
  • 79
  • 127
9

Apache Commons library has the appropriate method stripEnd.

halex
  • 16,253
  • 5
  • 58
  • 67
6
 public static String replaceAtTheEnd(String input){
    input = input.replaceAll("\\s+$", "");
    return input;
}
Eugene
  • 117,005
  • 15
  • 201
  • 306
5

I'd do it like this:

public static String trimEnd(String s)
{
    if ( s == null || s.length() == 0 )
        return s;
    int i = s.length();
    while ( i > 0 &&  Character.isWhitespace(s.charAt(i - 1)) )
        i--;
    if ( i == s.length() )
        return s;
    else
        return s.substring(0, i);
}

It's way more verbose than using a regular expression, but it's likely to be more efficient.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • I wouldn't be so sure about the efficiency thing (regexes might be coded in native code). And it's useless to reinvent the wheel, actually. Why code something manually that you can already do by libraries? You can reinvent the wheel only if what is available doesn't suit you (or is too slow). Optimize later ;) And I actually removed the downvote :P – m0skit0 Aug 27 '12 at 10:55
  • Obviously the only way to be sure about efficiency is to test against a meaningful data set. That said a regexp is likely to pay for its generality: for instance regexp engines usually translate input expressions for efficiency, which is a step my version doesn't have to perform. As for external libraries I tend to agree with you, but it's still a trade off between maintaining a small piece of code and keep track of yet another dependency. Why, sometimes it just takes less to code and debug something yourself than to look for a ready made solution! – Nicola Musatti Aug 27 '12 at 11:02
  • But you lose time writing this and testing it. My point is that you don't have to reinvent the wheel if not needed. – m0skit0 Aug 27 '12 at 11:05