打印格式化的字符串

时间:2013-08-27 06:54:53

标签: java string

代码是:

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>

这可能吗?

3 个答案:

答案 0 :(得分: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之后添加一个addiotional \ r。

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

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

答案 1 :(得分:2)

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

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>

答案 2 :(得分: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");