0

代码是:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "<html>" +
                "           <head>" +
                "               <title>" +
                "                   %s" +
                "               </title>" +
                "           </head>" +
                "           <body>" +
                "               %s" +
                "           </body>" +
                "   </html>";
        String str1 = String.format(str, "Home","Hallo");
        System.out.println(str1);
    }

我想打印str1如下

//The str1 should need to print like this


           <html>           
                <head>              
                    <title>                 
                        Home                
                    </title>        
                </head>     
                <body>              
                    Hallo           
                </body>
            </html>

这可能吗?

4

3 回答 3

5
String str = "<html>\n" +
"           <head>\n" +
"               <title>\n" +
"                   %s\n" +
"               </title>\n" +
"           </head>\n" +
"           <body>\n" +
"               %s\n" +
"           </body>\n" +
"   </html>\n";

这就是你想要的......如果你在 Windows 上,你可能需要在 \n 之后添加一个额外的 \r。

\n叫换行符,\r叫回车。这\n是 unix 和 windows 上的标准换行符,但 windows 下的某些程序可能需要回车 d 才能正确显示您的字符串。

System.getProperty("line.separator");\n在 unix 和\n\rwindows 上返回,它只是返回执行此命令的操作系统的标准“行分隔符”。

于 2013-08-27T06:58:11.243 回答
2

如果您想避免\n手动在大 HTML 字符串中到处添加,那么您可以OutputFormat.createPrettyPrint()dom4j 包中使用:

public String prettyHTMLPrint (String html) {  
    if (html==null || html.isEmpty()) {
        throw new RuntimeException("xml null or blank in prettyHTMLPrint()");
    }
    StringWriter sw;
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setSuppressDeclaration(true);
        org.dom4j.Document document = DocumentHelper.parseText(html);
        sw = new StringWriter();
        XMLWriter writer = new XMLWriter(sw, format);
        writer.write(document);
    }
    catch (Exception e) {
        throw new RuntimeException("Error pretty printing html: " + e, e);
    }
    return sw.toString();
}

对于您的示例,它会打印此格式化程序 HTML:

<html> 
  <head> 
    <title>Home</title> 
  </head>  
  <body>Hello</body> 
</html>
于 2013-08-27T07:48:45.390 回答
0

如果要设置硬编码的 2 个值,可以使用:

    StringBuilder builder = new StringBuilder();
    final String SEPARATOR = "\r\n";
    builder.append("<html>").append(SEPARATOR);
    builder.append("           <head>").append(SEPARATOR);
    builder.append("               <title>").append(SEPARATOR);
    builder.append("                   ").append("Home").append(SEPARATOR);
    builder.append("               </title>").append(SEPARATOR);
    builder.append("           </head>").append(SEPARATOR);
    builder.append("           <body>").append(SEPARATOR);
    builder.append("               ").append("Hallo").append(SEPARATOR);
    builder.append("           </body>").append(SEPARATOR);
    builder.append("   </html>").append(SEPARATOR);

动态的方式可能是某事。像这样:

    StringBuilder builder = new StringBuilder();
    final String SEPARATOR = "\r\n";
    builder.append("<html>").append(SEPARATOR);
    builder.append("           <head>").append(SEPARATOR);
    builder.append("               <title>").append(SEPARATOR);
    builder.append("                   %s1").append(SEPARATOR);
    builder.append("               </title>").append(SEPARATOR);
    builder.append("           </head>").append(SEPARATOR);
    builder.append("           <body>").append(SEPARATOR);
    builder.append("               %s2").append(SEPARATOR);
    builder.append("           </body>").append(SEPARATOR);
    builder.append("   </html>").append(SEPARATOR);

    String str = builder.toString().replace("%s1", "Home").replace("%s2", "Hallo");
于 2013-08-27T07:06:04.070 回答