Java-如何中断长语句

Java-如何中断长语句,java,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") 你可以 System.out.println("Lorem Ipsum is simply dummy text of the printing"

在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")
你可以

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");

生成的
字符串
将与字节码级别的原始字符串相同

使用字符串压缩,如

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");
尝试以下方法:

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")
使用“+”将位于两个不同行中的两个字符串连接起来。

您尝试过这种方法吗

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");

您还可以应用换行符,或者像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");

试试这个:
System.out.println(“Lorem Ipsum只是+
“印刷和排版行业。Lorem Ipsum一直是该行业的佼佼者”+

“标准虚拟tex”)

如果字符串为:

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");
比你今天能做的还多:

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 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只是印刷和排版行业的虚拟文本

注意:如果要按“”分割,它代表每个字符,所以

而不是

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");
拆分(“.”)

使用

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");
拆分(“\\”)

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");
拆分(模式。引号(“.”)

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

字符串#contains()


使用IDE并格式化此语句对不起,我是说在编辑器中。我使用的是TextWrangler,我的程序比屏幕还宽。@JigarJoshi:你真的建议下载一个完整的IDE来格式化一行字符串吗?你们一定是在开玩笑吧?不仅仅是为了这个目的,IDE比简单的文本编辑器更好用。IDE提供了详细的解决方案,让他学到更多+1.