Java tutorial
//package com.java2s; public class Main { public static String trimFirstAndLastNewline(String source) { source = source.replaceAll("^(\\\\n)*", ""); source = source.replaceAll("(\\\\n)*$", ""); return source; } }