I have a string which contains Java class as a string.
for e.g.
String class = "class MyClass{\n private int myInt;\n}";
How can I format this into a well indented ‘class string’?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Add a indent for every “{\n” and remove the indent for every “}”. Keep a counter of braces that will start at 0. After first “{“, braces =1. After “}” it becomes 0.
That way you’ll end up with this
Keep the same indent for all new lines.