Skip to content

Instantly share code, notes, and snippets.

@saayv1
Created August 22, 2022 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saayv1/f371756d4224cf0f055e3d6e9f17fdbf to your computer and use it in GitHub Desktop.
Save saayv1/f371756d4224cf0f055e3d6e9f17fdbf to your computer and use it in GitHub Desktop.
import java.util.*;
class MarkdownFormatted {
private void format(String input) {
String[] newLineStrings = input.split("\n");
ArrayList<ArrayList<String>> words = new ArrayList<>();
int size = newLineStrings[0].split("\\|").length;
int[] largest = new int[size];
for (int i = 0; i < largest.length; i++) {
largest[i] = -1;
}
for (int i = 0; i < newLineStrings.length; i++) {
String s = newLineStrings[i];
String[] s1 = s.split("\\|");
ArrayList<String> w = new ArrayList<>();
for (int j = 0; j < s1.length; j++) {
String gg = s1[j];
gg = gg.trim();
int length = gg.length();
if (length > largest[j]) {
largest[j] = length;
}
w.add(gg);
}
words.add(w);
}
System.out.println("\n\n");
for (int i = 0; i < words.size(); i++) {
for (int j = 1; j < largest.length; j++) {
int l1 = words.get(i).get(j).length();
if (i == 1) {
System.out.print("| " );
for (int k = 0; k < largest[j]; k++) {
System.out.print("-");
}
System.out.print(" ");
} else {
System.out.print("| " + words.get(i).get(j));
for (int k = 0; k <= largest[j] - l1; k++) {
System.out.print(" ");
}
}
}
System.out.println("|");
}
}
public static void main(String args[]) {
String input =
"|Syntax|Description|\n" +
"| --- | ----------- |\n" +
"| Header| Title |\n" +
"| Paragraph | Text|";
MarkdownFormatted m = new MarkdownFormatted();
m.format(input);
String input1 =
"|Syntax|Description|Quantity |\n" +
"| --- | ----------- |----|\n" +
"| Header| Title | 34|\n" +
"| Paragraph | Text| 170000|";
m.format(input1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment