Removing spaces at the end of a string in java
Solution 1
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.
Solution 2
Apache Commons library has the appropriate method stripEnd
.
Solution 3
String s = " this has spaces at the beginning and at the end ";
String result = s.replaceAll("\\s+$", "");
Solution 4
public static String replaceAtTheEnd(String input){
input = input.replaceAll("\\s+$", "");
return input;
}
Solution 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.
diminuta
Updated on September 05, 2020Comments
-
diminuta over 3 years
Possible Duplicate:
Strip Leading and Trailing Spaces From Java StringWhen 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...
-
oopsi over 11 yearsIs the string just one word, or can it be a sentence?
-
m0skit0 over 11 years@oopsi: doesn't matter actually ^^
-
-
m0skit0 over 11 yearsI wouldn't use * but +. Useless to replace 0 spaces ;)
-
Eugene over 11 years@m0skit0 :) It was first +, then I edit to *, have no idea why :). thx though
-
Marko Topolnik over 11 years
replaceFirst
would serve the purpose here as well. -
m0skit0 over 11 years@MarkoTopolnik: replaceFirst()? It's replaceLast() what the OP wants.
-
Marko Topolnik over 11 years@m0skit0 That regex can obviously have only a single match.
replaceLast
is not even a method ofString
. -
Nicola Musatti over 11 yearsMay I know the reason for the downvote?
-
m0skit0 over 11 yearsI 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 over 11 yearsI know it does not exist, it was a joke. Anyway the point is that
replaceFirst()
is no use here. -
Nicola Musatti over 11 yearsObviously 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!
-
m0skit0 over 11 yearsBut you lose time writing this and testing it. My point is that you don't have to reinvent the wheel if not needed.
-
Nactus over 8 yearsthe 2nd link doesn't work anymore