Java StringBuffer append new line example /*         Java StringBuffer append new line example         This example show...

Java StringBuffer append new line example

02:40 PASSOVE INCOME 0 Comments

Java StringBuffer append new line example

  1. /*
  2.         Java StringBuffer append new line example
  3.         This example shows how to append new line in StringBuffer in Java using
  4.         append method.
  5. */
  6. public class JavaStringBufferAppendNewLineExample {
  7.        
  8.         public static void main(String args[]){
  9.                
  10.                 //create StringBuffer object
  11.                 StringBuffer sbf = new StringBuffer("This is the first line.");
  12.                
  13.                 /*
  14.                  * To append new line to StringBuffer in Java, use
  15.                  * append method of StringBuffer class.
  16.                  *
  17.                  * Different operating systems uses different escape characters to
  18.                  * denote new line. For example, in Windows and DOS it is \r\n, in Unix
  19.                  * it is \n.
  20.                  *
  21.                  * In order to write code which works in all OS, use Java System property
  22.                  * line.separator instead of escape characters.
  23.                  */
  24.                
  25.                 sbf.append(System.getProperty("line.separator"));
  26.                 sbf.append("This is second line.");
  27.                
  28.                 System.out.println(sbf);
  29.                
  30.         }
  31. }
  32. /*
  33. Output of above given StringBuffer append new line Java example would be,
  34. This is the first line.
  35. This is second line.
  36. */

0 comments: