【问题标题】:How to create multiline strings in Java? [duplicate]如何在 Java 中创建多行字符串? [复制]
【发布时间】:2020-10-09 09:04:10
【问题描述】:

我知道scala使用stripMargin创建多行,例如:

def catalog = s"""{
        |"table":{"namespace":"default", "name":"table1"},
        |"rowkey":"key",
        |"columns":{
          |"col0":{"cf":"rowkey", "col":"key", "type":"string"},
          |"col1":{"cf":"cf1", "col":"col1", "type":"boolean"},
          |"col2":{"cf":"cf2", "col":"col2", "type":"double"},
          |"col3":{"cf":"cf3", "col":"col3", "type":"float"},
          |"col4":{"cf":"cf4", "col":"col4", "type":"int"},
          |"col5":{"cf":"cf5", "col":"col5", "type":"bigint"},
          |"col6":{"cf":"cf6", "col":"col6", "type":"smallint"},
          |"col7":{"cf":"cf7", "col":"col7", "type":"string"},
          |"col8":{"cf":"cf8", "col":"col8", "type":"tinyint"}
        |}
      |}""".stripMargin

Java 有这么酷的语法吗?

谢谢!

【问题讨论】:

标签: java scala


【解决方案1】:

从 java 13 开始,您可以使用多行 java 字符串,例如:

class Foo { 
 public void bar() {    
   String txt = """      
     Some        
       Nested          
         Text        
       Is     
     Here      
   """;  
 }
}

更多信息请访问:https://www.jrebel.com/blog/using-text-blocks-in-java-13

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 2012-07-23
    • 1970-01-01
    • 2019-09-22
    • 2017-10-10
    • 2011-08-08
    • 2011-05-05
    • 1970-01-01
    • 2019-05-09
    相关资源
    最近更新 更多