regex - Delete all the space at end of each line in Java -


i'm trying delete spaces , tabs @ end of lines.

i use following methods:

string.replace("\\s+$", ""); 

here, "\s+$" regex expression

it seems it' right. fact not delete it.

the string is:

               a) aaaaaa.                                    b) bbbbb.            c) cccccc.            d) ddddd. 

like this:

string.replaceall("\\s+$", ""); 
  • string#replace() not take regex argument.
  • you need double escape \s

edit: yes work:

public static void main(final string[] args) {     final string = "     aaaaaaaa             ";     system.out.println(a.replaceall("\\s+$", "")); } 

maybe confusion think a.replaceall() modify string a. strings in java immutable (they cannot change). a.replaceall() return modified string.

edit2: if you're using multiline string, "\\s+[$\n]" regex should work.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -