Java - 如何打破冗长的声明

时间:2015-02-15 17:50:13

标签: java

如何在Java中将这么长的代码分成几行?

System.out.println("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy tex")

7 个答案:

答案 0 :(得分:3)

你试过这个吗?

System.out.println("Lorem Ipsum is simply dummy text"
                   + " of the printing and typesetting industry. "
                   + "Lorem Ipsum has been the industry's "
                   + "standard dummy tex");

答案 1 :(得分:1)

你可以做到

System.out.println("Lorem Ipsum is simply dummy text of the printing" +  
                    "and typesetting industry. Lorem Ipsum has been " + 
                    "the industry's standard dummy tex");

结果String将与字节码级别的原始文件相同

答案 2 :(得分:1)

使用字符串连接,例如

System.out.println("Lorem Ipsum is simply dummy text of the printing and typesetting industry." +
        " Lorem Ipsum has been the industry's standard dummy tex");

答案 3 :(得分:1)

尝试类似:

System.out.println("Lorem Ipsum is simply dummy text of the printing and" + 
" typesetting industry. Lorem Ipsum has been the industry's" +
" standard dummy tex")

使用“+”连接两个不同行的字符串。

答案 4 :(得分:1)

您也可以应用换行符 - 或者使用多个命令(如Rei执行此操作),但每个部分都使用print,最后一个将是println,或者使用\n特殊字符:

System.out.println("Lorem Ipsum is simply dummy text of the printing\n" +  
                "and typesetting industry. Lorem Ipsum has been\n " + 
                "the industry's standard dummy tex");

答案 5 :(得分:1)

试试这个:System.out.println("Lorem Ipsum is simply dummy text of the "+ "printing and typesetting industry. Lorem Ipsum has been the industry's "+ "standard dummy tex");

答案 6 :(得分:1)

如果你有String as:

System.out.println("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy tex");

你可以做到这一点vay:

System.out.println("Lorem Ipsum is simply dummy text of the printing and typesetting industry."  
                  +"Lorem Ipsum has been the industry's standard dummy tex");

或者您可以拆分字符串: 添加要拆分String的任何字符或使用String中已有的一些字符。

String string1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.-Lorem Ipsum has been the industry's standard dummy tex";

String[] parts = string1.split("-");
String part0 = parts[0]; // Lorem Ipsum is simply dummy text of the printing and typesetting industry.
String part1 = parts[1]; //Lorem Ipsum has been the industry's standard dummy tex
System.out.println(part0);

输出:Lorem Ipsum只是印刷和排版行业的虚拟文本。

注意:如果您想按“。”进行拆分,则代表每个字符

而不是

  

分裂( “”);

使用

  

分裂( “\\。”);

  

分裂(Pattern.quote() “”);

要预先测试字符串是否包含 - ,只需使用

  

字符串#含有()

if (string.contains("-")) {
    // Split it. 
} 
else {
  throw new IllegalArgumentException("String " + string1 + " does not contain-"); 
 }