java-up
java-up copied to clipboard
文字块输出问题
老师为啥我这两个输出是false?
String poetryHtml =
"<html>\n" +
"<head></head>\n" +
"<body>\n" +
"<div align=\"center\">\n" +
" No man is an island,<br />\n" +
" Entire of itself,<br />\n" +
" Every man is a piece of the continent,<br />\n" +
" A part of the main.\n" +
"</div>\n" +
"</body>\n" +
"</html>\n"; /// 此处少了一个换行.
System.out.println(poetryHtml);
System.out.println();
String poetryTextArea = """
<html>
<head></head>
<body>
<div align="center">
No man is an island,<br />
Entire of itself,<br />
Every man is a piece of the continent,<br />
A part of the main.
</div>
</body>
</html>
""";
System.out.println(poetryTextArea);
System.out.println(poetryHtml.equals(poetryTextArea));
System.out.println(poetryHtml==poetryTextArea);
你的poetryHtml 少了一个换行符.
另外一种方案:
String poetryHtml =
"<html>\n" +
"<head></head>\n" +
"<body>\n" +
"<div align=\"center\">\n" +
" No man is an island,<br />\n" +
" Entire of itself,<br />\n" +
" Every man is a piece of the continent,<br />\n" +
" A part of the main.\n" +
"</div>\n" +
"</body>\n" +
"</html>";
System.out.println(poetryHtml);
System.out.println();
String poetryTextArea = """
<html>
<head></head>
<body>
<div align="center">
No man is an island,<br />
Entire of itself,<br />
Every man is a piece of the continent,<br />
A part of the main.
</div>
</body>
</html>"""; /// 这个地方结尾改成这样也可以.
System.out.println(poetryTextArea);
System.out.println(poetryHtml.equals(poetryTextArea));
System.out.println(poetryHtml==poetryTextArea);
另外一种方案:
String poetryHtml = "<html>\n" + "<head></head>\n" + "<body>\n" + "<div align=\"center\">\n" + " No man is an island,<br />\n" + " Entire of itself,<br />\n" + " Every man is a piece of the continent,<br />\n" + " A part of the main.\n" + "</div>\n" + "</body>\n" + "</html>"; System.out.println(poetryHtml); System.out.println(); String poetryTextArea = """ <html> <head></head> <body> <div align="center"> No man is an island,<br /> Entire of itself,<br /> Every man is a piece of the continent,<br /> A part of the main. </div> </body> </html>"""; /// 这个地方结尾改成这样也可以. System.out.println(poetryTextArea); System.out.println(poetryHtml.equals(poetryTextArea)); System.out.println(poetryHtml==poetryTextArea);
thanks