java-up icon indicating copy to clipboard operation
java-up copied to clipboard

文字块输出问题

Open wxywizard opened this issue 4 years ago • 3 comments

老师为啥我这两个输出是false?

wxywizard avatar Nov 23 '21 16:11 wxywizard

        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 少了一个换行符.

pruidong avatar Nov 23 '21 17:11 pruidong

另外一种方案:


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

pruidong avatar Nov 23 '21 17:11 pruidong

另外一种方案:

        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

wxywizard avatar Nov 23 '21 17:11 wxywizard