1、在jdk 8之前版本,可以使用 + 连接字符串
2、StringBuilder 进行append操作
3、string.concat进行操作
4、string format进行插空
5、jdk 8 s可以使用String ,join 进行字符串连接
6、java.util.StringJoiner 操作
7、使用 StringJoiner
8、 using Collectors#joining()
9、Starting with JDK 13/15using text blocks
- package chaptor01.P01_TextBlockSQLJSONHTML;
-
- import java.util.StringJoiner;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
-
- public class Main {
- public static void main(String[] args) {
- //字符串连接
- // 在jdk 8 之前,我们使用+进行字符串连接
- System.out.println("Multiline SQL/JSON/HTML before text blocks "
- + "(using concatenation via the '+' operator):");
- String sql1
- = "UPDATE \"public\".\"office\"\n"
- + "SET (\"address_first\", \"address_second\", \"phone\") =\n"
- + " (SELECT \"public\".\"employee\".\"first_name\",\n"
- + " \"public\".\"employee\".\"last_name\", ?\n"
- + " FROM \"public\".\"employee\"\n"
- + " WHERE \"public\".\"employee\".\"job_title\" = ?";
-
- String json1
- = "{\n"
- + " \"widget\": {\n"
- + " \"debug\": \"on\",\n"
- + " \"window\": {\n"
- + " \"title\": \"Sample Widget 1\",\n"
- + " \"name\": \"back_window\"\n"
- + " },\n"
- + " \"image\": {\n"
- + " \"src\": \"images\\sw.png\"\n"
- + " },\n"
- + " \"text\": {\n"
- + " \"data\": \"Click Me\",\n"
- + " \"size\": 39\n"
- + " }\n"
- + " }\n"
- + "}";
-
- String html1
- = "
\n"- + "
\n"- + "
Name \n" - + "
Age \n" - + "
\n" - + "
\n"- + "
John \n" - + "
Smith \n" - + "
22 \n" - + "
\n" - + "
";-
- System.out.println(sql1);
- System.out.println();
- System.out.println(json1);
- System.out.println();
- System.out.println(html1);
-
- // 我们同样可以 StringBuilder 连接 append 连接
- System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using StringBuilder):");
- StringBuilder sql2 = new StringBuilder();
- sql2.append("UPDATE \"public\".\"office\"\n")
- .append("SET (\"address_first\", \"address_second\", \"phone\") =\n")
- .append(" (SELECT \"public\".\"employee\".\"first_name\",\n")
- .append(" \"public\".\"employee\".\"last_name\", ?\n")
- .append(" FROM \"public\".\"employee\"\n")
- .append(" WHERE \"public\".\"employee\".\"job_title\" = ?;");
-
- StringBuilder json2 = new StringBuilder();
- json2.append("{\n")
- .append(" \"widget\": {\n")
- .append(" \"debug\": \"on\",\n")
- .append(" \"window\": {\n")
- .append(" \"title\": \"Sample Widget 1\",\n")
- .append(" \"name\": \"back_window\"\n")
- .append(" },\n")
- .append(" \"image\": {\n")
- .append(" \"src\": \"images\\sw.png\"\n")
- .append(" },\n")
- .append(" \"text\": {\n")
- .append(" \"data\": \"Click Me\",\n")
- .append(" \"size\": 39\n")
- .append(" }\n")
- .append(" }\n")
- .append("}");
-
- StringBuilder html2 = new StringBuilder();
- html2.append("
\n")- .append("
\n")- .append("
Name \n") - .append("
Age \n") - .append("
\n") - .append("
\n")- .append("
John \n") - .append("
Smith \n") - .append("
22 \n") - .append("
\n") - .append("
");-
- System.out.println(sql2);
- System.out.println();
- System.out.println(json2);
- System.out.println();
- System.out.println(html2);
-
- // using String#concat()
- System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using String#concat()):");
- String sql3 = "UPDATE \"public\".\"office\"\n"
- .concat("SET (\"address_first\", \"address_second\", \"phone\") =\n")
- .concat(" (SELECT \"public\".\"employee\".\"first_name\",\n")
- .concat(" \"public\".\"employee\".\"last_name\", ?\n")
- .concat(" FROM \"public\".\"employee\"\n")
- .concat(" WHERE \"public\".\"employee\".\"job_title\" = ?;");
-
- String json3 = "{\n"
- .concat(" \"widget\": {\n")
- .concat(" \"debug\": \"on\",\n")
- .concat(" \"window\": {\n")
- .concat(" \"title\": \"Sample Widget 1\",\n")
- .concat(" \"name\": \"back_window\"\n")
- .concat(" },\n")
- .concat(" \"image\": {\n")
- .concat(" \"src\": \"images\\sw.png\"\n")
- .concat(" },\n")
- .concat(" \"text\": {\n")
- .concat(" \"data\": \"Click Me\",\n")
- .concat(" \"size\": 39\n")
- .concat(" }\n")
- .concat(" }\n")
- .concat("}");
-
- String html3 = "
\n"- .concat("
\n")- .concat("
Name \n") - .concat("
Age \n") - .concat("
\n") - .concat("
\n")- .concat("
John \n") - .concat("
Smith \n") - .concat("
22 \n") - .concat("
\n") - .concat("
");-
- System.out.println(sql3);
- System.out.println();
- System.out.println(json3);
- System.out.println();
- System.out.println(html3);
-
- // 使用 string format连接
- System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using String#format()):");
- String sql4 = String.format("%s%s%s%s%s%s",
- "UPDATE \"public\".\"office\"\n",
- "SET (\"address_first\", \"address_second\", \"phone\") =\n",
- " (SELECT \"public\".\"employee\".\"first_name\",\n",
- " \"public\".\"employee\".\"last_name\", ?\n",
- " FROM \"public\".\"employee\"\n",
- " WHERE \"public\".\"employee\".\"job_title\" = ?;");
-
- String json4 = String.format("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
- "{\n",
- " \"widget\": {\n",
- " \"debug\": \"on\",\n",
- " \"window\": {\n",
- " \"title\": \"Sample Widget 1\",\n",
- " \"name\": \"back_window\"\n",
- " },\n",
- " \"image\": {\n",
- " \"src\": \"images\\sw.png\"\n",
- " },\n",
- " \"text\": {\n",
- " \"data\": \"Click Me\",\n",
- " \"size\": 39\n",
- " }\n",
- " }\n",
- "}");
-
- String html4 = String.format("%s%s%s%s%s%s%s%s%s%s%s",
- "
\n",- "
\n",- "
Name \n", - "
Age \n", - "
\n", - "
\n",- "
John \n", - "
Smith \n", - "
22 \n", - "
\n", - "
");-
- System.out.println(sql4);
- System.out.println();
- System.out.println(json4);
- System.out.println();
- System.out.println(html4);
-
- // jdk 8 s可以使用String ,join 进行字符串连接
- System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using String#join()):");
- String sql5 = String.join("\n",
- "UPDATE \"public\".\"office\"",
- "SET (\"address_first\", \"address_second\", \"phone\") =",
- " (SELECT \"public\".\"employee\".\"first_name\",",
- " \"public\".\"employee\".\"last_name\", ?",
- " FROM \"public\".\"employee\"",
- " WHERE \"public\".\"employee\".\"job_title\" = ?;");
-
- String json5 = String.join("\n",
- "{",
- " \"widget\": {",
- " \"debug\": \"on\",",
- " \"window\": {",
- " \"title\": \"Sample Widget 1\",",
- " \"name\": \"back_window\"",
- " },",
- " \"image\": {",
- " \"src\": \"images\\sw.png\"",
- " },",
- " \"text\": {",
- " \"data\": \"Click Me\",",
- " \"size\": 39",
- " }",
- " }",
- "}");
-
- String html5 = String.join("\n",
- "
",- "
",- "
Name ", - "
Age ", - "
", - "
",- "
John ", - "
Smith ", - "
22 ", - "
", - "
");-
- System.out.println(sql5);
- System.out.println();
- System.out.println(json5);
- System.out.println();
- System.out.println(html5);
-
- // 使用 StringJoiner
- System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using StringJoiner):");
- StringJoiner sql6 = new StringJoiner("\n");
- sql6.add("UPDATE \"public\".\"office\"")
- .add("SET (\"address_first\", \"address_second\", \"phone\") =")
- .add(" (SELECT \"public\".\"employee\".\"first_name\",")
- .add(" \"public\".\"employee\".\"last_name\", ?")
- .add(" FROM \"public\".\"employee\"")
- .add(" WHERE \"public\".\"employee\".\"job_title\" = ?;");
-
- StringJoiner json6 = new StringJoiner("\n");
- json6.add("{")
- .add(" \"widget\": {")
- .add(" \"debug\": \"on\",")
- .add(" \"window\": {")
- .add(" \"title\": \"Sample Widget 1\",")
- .add(" \"name\": \"back_window\"")
- .add(" },")
- .add(" \"image\": {")
- .add(" \"src\": \"images\\sw.png\"")
- .add(" },")
- .add(" \"text\": {")
- .add(" \"data\": \"Click Me\",")
- .add(" \"size\": 39")
- .add(" }")
- .add(" }")
- .add("}");
-
- StringJoiner html6 = new StringJoiner("\n");
- html6.add("
")- .add("
")- .add("
Name ") - .add("
Age ") - .add("
") - .add("
")- .add("
John ") - .add("
Smith ") - .add("
22 ") - .add("
") - .add("
");-
- System.out.println(sql6);
- System.out.println();
- System.out.println(json6);
- System.out.println();
- System.out.println(html6);
-
-
- // using Collectors#joining()
- System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using Collectors#joining()):");
- String sql7 = Stream.of(
- "UPDATE \"public\".\"office\"",
- "SET (\"address_first\", \"address_second\", \"phone\") =",
- " (SELECT \"public\".\"employee\".\"first_name\",",
- " \"public\".\"employee\".\"last_name\", ?",
- " FROM \"public\".\"employee\"",
- " WHERE \"public\".\"employee\".\"job_title\" = ?;")
- .collect(Collectors.joining(String.valueOf("\n")));
-
- String json7 = Stream.of(
- "{",
- " \"widget\": {",
- " \"debug\": \"on\",",
- " \"window\": {",
- " \"title\": \"Sample Widget 1\",",
- " \"name\": \"back_window\"",
- " },",
- " \"image\": {",
- " \"src\": \"images\\sw.png\"",
- " },",
- " \"text\": {",
- " \"data\": \"Click Me\",",
- " \"size\": 39",
- " }",
- " }",
- "}")
- .collect(Collectors.joining(String.valueOf("\n")));
-
- String html7 = Stream.of(
- "
",- "
",- "
Name ", - "
Age ", - "
", - "
",- "
John ", - "
Smith ", - "
22 ", - "
", - "
")- .collect(Collectors.joining(String.valueOf("\n")));
-
- System.out.println(sql7);
- System.out.println();
- System.out.println(json7);
- System.out.println();
- System.out.println(html7);
-
- // Starting with JDK 13/15
- // using text blocks
- System.out.println("\n Multiline SQL/JSON/HTML using text blocks:");
- String sql8 = """
- UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ?""";
-
- String json8 = """
- {
- "widget": {
- "debug": "on",
- "window": {
- "title": "Sample Widget 1",
- "name": "back_window"
- },
- "image": {
- "src": "images\\sw.png"
- },
- "text": {
- "data": "Click Me",
- "size": 39
- }
- }
- }""";
-
- String html8 = """
-
-
-
Name -
Age -
-
-
John -
Smith -
22 -
-
""";-
- System.out.println(sql8);
- System.out.println();
- System.out.println(json8);
- System.out.println();
- System.out.println(html8);
-
-
- }
- }
2、举例说明文本块分隔符的用法
这个语法跟python的类似
- public class Main {
-
- public static void main(String[] args) {
-
- System.out.println("The common usage of text block delimiters:\n");
- String sql1 = """
- UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ?""";
-
- System.out.println("-- BEFORE TEXT BLOCK --");
- System.out.println(sql1);
- System.out.println("-- AFTER TEXT BLOCK --");
-
- System.out.println("\nKeep the opening delimiter and the content on the "
- + "same line is not accepted (does not compile)\n");
- /*
- String not_compile = """UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ?""";
- */
-
- System.out.println("\nShift-left the content has no effect:\n");
- String sql2 = """
- UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ?""";
-
- System.out.println("-- BEFORE TEXT BLOCK --");
- System.out.println(sql2);
- System.out.println("-- AFTER TEXT BLOCK --");
-
- System.out.println("\nShift-right the opening/closing delimiter has no effect:\n");
- String sql3 = """
- UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ? """;
-
- System.out.println("-- BEFORE TEXT BLOCK --");
- System.out.println(sql3);
- System.out.println("-- AFTER TEXT BLOCK --");
-
- System.out.println("\nMoving the closing delimiter on its own line (1):\n");
- String sql4 = """
- UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ?
- """;
-
- System.out.println("-- BEFORE TEXT BLOCK --");
- System.out.println(sql4);
- System.out.println("-- AFTER TEXT BLOCK --");
-
- System.out.println("\nMoving the closing delimiter on its own line (2):\n");
- String sql5 = """
- UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ?
- """;
-
- System.out.println("-- BEFORE TEXT BLOCK --");
- System.out.println(sql5);
- System.out.println("-- AFTER TEXT BLOCK --");
-
- System.out.println("\nMoving on its own line and shift-left the closing delimiter:\n");
- String sql6 = """
- UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ?
- """;
-
- System.out.println("-- BEFORE TEXT BLOCK --");
- System.out.println(sql6);
- System.out.println("-- AFTER TEXT BLOCK --");
-
- System.out.println("\nMoving on its own line and shift-right the closing delimiter:\n");
- String sql7 = """
- UPDATE "public"."office"
- SET ("address_first", "address_second", "phone") =
- (SELECT "public"."employee"."first_name",
- "public"."employee"."last_name", ?
- FROM "public"."employee"
- WHERE "public"."employee"."job_title" = ?
- """;
-
- System.out.println("-- BEFORE TEXT BLOCK --");
- System.out.println(sql7);
- System.out.println("-- AFTER TEXT BLOCK --");
- }
- }
-
相关阅读:
外包干了一个月,技术明显进步。。。。。
SciChart WPF Charts SDK v6.x
设计模式 14 模板模式
kubernetes集群搭建
期货怎么克服频繁止损(期货交易怎么止损)
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
美团2017年CodeM大赛-资格赛 NC13224 送外卖(记忆化搜索 or 预处理 dfs)
文件包含漏洞详解
[AI]Python中的Restful
【解决方法】错误:无法和SFTP服务器建立FTP连接,请选择合适的协议
-
原文地址:https://blog.csdn.net/wangjunji34478/article/details/133392469
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU