要将JavaScript代码转换为一个大的Java字符串,您可以使用以下方法:
StringBuilder
类:StringBuilder sb = new StringBuilder();
sb.append("public class HelloWorld {\n");
sb.append("\tpublic static void main(String[] args) {\n");
sb.append("\t\tSystem.out.println(\"Hello, World!\");\n");
sb.append("\t}\n");
sb.append("}");
String javaCode = sb.toString();
String.format()
方法:String javaCode = String.format("public class HelloWorld {\n" +
"\tpublic static void main(String[] args) {\n" +
"\t\tSystem.out.println(\"Hello, World!\");\n" +
"\t}\n" +
"}");
String
字面量:String javaCode = "public class HelloWorld {\n" +
"\tpublic static void main(String[] args) {\n" +
"\t\tSystem.out.println(\"Hello, World!\");\n" +
"\t}\n" +
"}";
StringBuilder
类和append()
方法:StringBuilder sb = new StringBuilder();
sb.append("public class HelloWorld {\n");
sb.append("\tpublic static void main(String[] args) {\n");
sb.append("\t\tSystem.out.println(\"Hello, World!\");\n");
sb.append("\t}\n");
sb.append("}");
String javaCode = sb.toString();
String.join()
方法:List<String> lines = Arrays.asList("public class HelloWorld {",
"\tpublic static void main(String[] args) {",
"\t\tSystem.out.println(\"Hello, World!\");",
"\t}",
"}");
String javaCode = String.join("\n", lines);
这些方法都可以将JavaScript代码转换为一个大的Java字符串。
领取专属 10元无门槛券
手把手带您无忧上云