Java tutorial
//package com.java2s; //* Licensed Materials - Property of IBM * import java.util.StringTokenizer; public class Main { /** * Remove leading a trailing whitespace characters from each line of input * @param input * @return */ public static String trim(String input) { final String newlineDelimiters = "\n\r\f"; StringBuilder ret = new StringBuilder(); StringTokenizer st = new StringTokenizer(input, newlineDelimiters); while (st.hasMoreTokens()) { ret.append(st.nextToken().replaceAll("^\\s+", "").replaceAll("\\s+$", "")); ret.append('\n'); } return ret.toString(); } }