Skip to main content
added 67 characters in body
Source Link
everag
  • 7.7k
  • 2
  • 32
  • 45

ThisIf you really want to do this with Regex, this probably would do the trick for you

String str = " \n a b c \n 1 2 3 \n x y z ";
    
str = str.replaceAll("^\s|\s$|\\n""^\\s|\n\\s|\\s$", "");

This probably would do the trick for you

str.replaceAll("^\s|\s$|\\n", "");

If you really want to do this with Regex, this probably would do the trick for you

String str = " \n a b c \n 1 2 3 \n x y z ";
    
str = str.replaceAll("^\\s|\n\\s|\\s$", "");
Post Undeleted by everag
Post Deleted by everag
Source Link
everag
  • 7.7k
  • 2
  • 32
  • 45

This probably would do the trick for you

str.replaceAll("^\s|\s$|\\n", "");