我想格式化包含以下代码的字符串:
"function() { for(i=0;i<n;i++){ return i; }}"
关键是要为每个人添加新的线条;除了在for循环中,所以格式化的输出应该是这样的:
function() {
for(i=0;i<n;i++){
return i;
}
}
是否有任何书面函数,或者我必须遍历整个字符串?
你必须使用google-java-format-1.5.jar
和google-java-format-1.5-all-deps.jar
并查看以下示例代码
public class Main {
public static void main(String[] args) {
Formatter formatter = new Formatter();
try {
System.out.println(formatter.formatSource("public class Test{public static void main(String[] args) {String s;}}"));
} catch (FormatterException e) {
e.printStackTrace();
}
}
}
和输出将是
public class Test {
public static void main(String[] args) {
String s;
}
}
希望这会对你有所帮助