0

The code is:

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

I want to print the str1 as follows

//The str1 should need to print like this


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

Is this possible?

CosminO
  • 5,018
  • 6
  • 28
  • 50
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55

3 Answers3

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

this is what you want... if you are on windows, you might need to add an addiotional \r after the \n.

\n is called the newline character, \ris called carriage return. The \n is the standard newline character on unix and windows, but some programs under windows might need the carriage return dto display your string properly.

System.getProperty("line.separator"); will return \n on unix and \n\r on windows, it simply returns the standard "line separator" of the operating system this command is executed on.

Theolodis
  • 4,977
  • 3
  • 34
  • 53
2

If you want to avoid adding \n all over the place in big HTML string manually then you can use OutputFormat.createPrettyPrint() in dom4j package:

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

For your example it prints this formatter HTML:

<html> 
  <head> 
    <title>Home</title> 
  </head>  
  <body>Hello</body> 
</html>
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

If you want to set the 2 Values hardcoded, you can use this:

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

The dynamic way could be sth. like this:

    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");
JavaDM
  • 851
  • 1
  • 6
  • 29