Java String align multiple line by separator
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String multiline = "ab.c\nde.fg\ndemo2s.com"; String alignBy = "."; System.out.println(align(multiline, alignBy)); }/*from w w w.j a v a 2 s . co m*/ /** * Vertically align multi-line text according to the position of a reference symbol. * See main() below for a demonstration. */ public static String align(String multiline, String alignBy, int from) { String[] lines = multiline.split("\n"); int[] poses = new int[lines.length]; final int NOMATCH = -1; for (;;) { int furthest = NOMATCH; for (int i = 0; i < lines.length; i++) { int pos = lines[i].indexOf(alignBy, from); poses[i] = pos; furthest = Math.max(furthest, pos); } if (furthest == NOMATCH) break; for (int i = 0; i < lines.length; i++) { if (poses[i] != NOMATCH) { int reps = furthest - poses[i]; String repl = repeat(" ", reps); lines[i] = insertBefore(lines[i], repl, poses[i]); } } from = furthest + alignBy.length(); } return combineLines(lines); } /** * Align multi-line text according to the position of a reference symbol. */ public static String align(String multiline, String alignBy) { return align(multiline, alignBy, 0); } /** * Repeat a string. */ public static String repeat(String str, int count) { if (count <= 0) return ""; StringBuilder sb = new StringBuilder(str.length() * count); for (int i = 0; i < count; i++) sb.append(str); return sb.toString(); } /** * Insert substring, shifting forward existing characters at that position. */ public static String insertBefore(String str, String substr, int pos) { return new StringBuilder(str).insert(pos, substr).toString(); } /** * Combine an array of strings into multiline text. */ public static String combineLines(String[] strings) { StringBuilder sb = new StringBuilder(); for (String str : strings) { sb.append(str); sb.append("\n"); } return sb.toString(); } }
//package com.demo2s; import java.util.List; import java.util.stream.Stream; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; public class Main { public static void main(String[] argv) throws Exception { String multiLine = " 11 & 12 \n & 23 \n & 23\n & demo2s.com"; System.out.println(align(multiLine)); }/*from w ww . j a va 2 s. c o m*/ public static String align(String multiLine) { return align(multiLine, "&"); } public static String align(String multiLine, String regex) { List<List<String>> table = split(multiLine, "\\n").map(line -> split(line, regex).collect(toList())) .collect(toList()); return formatTable(table); } public static Stream<String> split(String string, String regex) { return split(string, regex, 0); } public static Stream<String> split(String string, String regex, int limit) { if (string == null || regex == null) { return Stream.of(); } return Stream.of(string.split(regex, limit)); } /** * format 2-dim string table,left aligned */ public static String formatTable(List<List<String>> table) { Integer max = table.stream().map(List::size).max(Integer::compare).get(); StringBuffer[] sb = new StringBuffer[table.size()]; for (int i = 0; i < sb.length; i++) { sb[i] = new StringBuffer(); } for (int j = 0; j < max; j++) { for (int i = 0; i < table.size(); i++) { if (j < table.get(i).size()) { sb[i].append(table.get(i).get(j).trim()); } } Integer current = Stream.of(sb).map(StringBuffer::length).max(Integer::compare).get(); Stream.of(sb).forEach(s -> s.append(String.format("%" + (current - s.length() + 1) + "s", " "))); } return Stream.of(sb).map(StringBuffer::toString).map(s -> s.replaceAll("\\s+$", "")).collect(joining("\n")); } }