Communities for your favorite technologies. Explore all Collectives
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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$", "");