Skip to content

Instantly share code, notes, and snippets.

@Kehet
Created November 4, 2012 01:32
Show Gist options
  • Save Kehet/4009728 to your computer and use it in GitHub Desktop.
Save Kehet/4009728 to your computer and use it in GitHub Desktop.
toString -string formatter
package helpers;
/**
* This class formats general toString -string to more readable form
*
* Usage:
* ToString.print(object);
*
* @author Kehet
*/
public class ToString {
public static void print(Object object) {
ToString.print(object.toString());
}
public static void print(String toString) {
Integer indentation = 0;
Integer newLineNext = -1;
boolean ignore = false;
for (int i = 0; i < toString.length(); i++) {
String mark = toString.substring(i, i + 1);
if (mark.equals("'")) {
if (ignore) {
ignore = false;
} else {
ignore = true;
}
} else if ((mark.equals("{") || mark.equals("[")) && !ignore) {
indentation++;
newLineNext = 2;
} else if ((mark.equals("}") || mark.equals("]")) && !ignore) {
indentation--;
newLineNext = 1;
} else if (mark.equals(",") && !ignore) {
newLineNext = 3;
}
if (newLineNext == 1) {
System.out.println();
for (int j = 0; j < indentation * 4; j++) {
System.out.print(" ");
}
}
System.out.print(mark);
if (newLineNext > 0) {
newLineNext--;
}
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment