public static String stripBlankLines(String str) {
    StringBuilder builder = new StringBuilder();
    String[] split = str.split("\n");
    for (String s : split) {
      if (!s.trim().isEmpty()) {
        builder.append(s).append('\n');
      }
    }
    if (builder.charAt(builder.length() - 1) == '\n') {
      return builder.toString();
    } else {
      return builder.substring(0, builder.length() - 1);
    }
  }
@Test
  public void testStripBlankLines() {
    assertEquals(StringUtils.stripBlankLines("a\n \nb\n"), "a\nb\n");
  }