public static String trim (String s) {
    if (s == null || s.length () == 0) return s;

    int from = 0;
    while (from < s.length ())
      if (Character.isWhitespace (s.charAt (from))) from++;
      else break;
    if (from == s.length ()) return "";

    int to = s.length () - 1;
    while (to > 0)
      if (Character.isWhitespace (s.charAt (to))) to--;
      else break;

    return s.substring (from, to+1);
  }