How to print strings with line breaks in java

前端 未结 9 647
刺人心
刺人心 2020-12-01 17:10

I need to print a string using java so I fond the following solution After googled a lot. I have done some changes to print the string without showing the print dialog. My p

相关标签:
9条回答
  • 2020-12-01 17:58

    Do this way:-

    String newline = System.getProperty("line.separator");
    
    private static final String mText = "SHOP MA" + newline +
            + "----------------------------" + newline +
            + "Pannampitiya" + newline +
            + "09-10-2012 harsha  no: 001" + newline +
            + "No  Item  Qty  Price  Amount" + newline +
            + "1 Bread 1 50.00  50.00" + newline +
            + "____________________________" + newline;
    
    0 讨论(0)
  • 2020-12-01 18:01
    String newline = System.getProperty("line.separator");
    System.out.println("First line" + newline);
    System.out.println("Second line" + newline);
    System.out.println("Third line");
    
    0 讨论(0)
  • 2020-12-01 18:01

    You can try using StringBuilder: -

        final StringBuilder sb = new StringBuilder();
    
        sb.append("SHOP MA\n");
        sb.append("----------------------------\n");
        sb.append("Pannampitiya\n");
        sb.append("09-10-2012 harsha  no: 001\n");
        sb.append("No  Item  Qty  Price  Amount\n");
        sb.append("1 Bread 1 50.00  50.00\n");
        sb.append("____________________________\n");
    
        // To use StringBuilder as String.. Use `toString()` method..
        System.out.println(sb.toString());   
    
    0 讨论(0)
提交回复
热议问题