I have a string like this :
Mr Moh Jo\n
Address\n
33333 City\n\n
Aland Islands
and I would like to delete whitespaces in the beginning of each line and end the end of the each line with following code but it didn't work
public static String trimWhiteSpaceFromTheBeginingAndEndOFTheLine(
String string) {
Pattern trimmer = Pattern.compile("^\\s+|\\s+$");
Matcher m = trimmer.matcher(string);
StringBuffer out = new StringBuffer();
while (m.find())
m.appendReplacement(out, "");
m.appendTail(out);
return out.toString();
}
Expected result:
Mr Moh Jo\n
Address\n
33333 City\n\n
Aland Islands