I wanted to print the exception message on JSP page, So I am making use of StringBuilder.append() API to print the exception in string format. But the problem is that, I am unable to insert the tab space at the beginning of every exception line, like :
StringBuilder sb = new StringBuilder();
StackTraceElement[] stackTraceElements = logMessage.getStackTrace();
for(StackTraceElement stackTraceElement:stackTraceElements) {
sb.append("\t").append("at ").append(stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + " : " + stackTraceElement.getLineNumber() + ")").append("<br>");
}
Tab space is not added even after adding sb.append("\t"), rather it is only taking single space. Can anyone please help regarding this issue.
StringBuilder sb = new StringBuilder();
sb.append("\t").append("val2");
There's nothing wrong with StringBuilder API, at least when the result is used inside Java. We'll get a perfectly normal tab at the beginning, as expected.
I think this has something to do with the fact that all tab characters are treated as whitespace by parsers and are turned into a single space char. Please refer to this: How to get a tab character?