1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* Java StringBuffer setLength Example This example shows how to set length of StringBuffer using setLength method of StringBuffer class in Java. */ public class JavaStringBufferSetLengthExample { public static void main(String[] args) { //create StringBuffer object StringBuffer sbf = new StringBuffer("StringBuffer setLength method example"); /* * To set length of StringBuffer, use * void setLength(int newLength) method of * StringBuffer class. * * If newLegth is less than the original length, contents of * StringBuffer would be truncated. * * If newLength is grater than the original length, StringBuffer * would be filled with null characters ('u0000'). */ sbf.setLength(12); System.out.println("StringBuffer contents: " + sbf); /* * To delete or clear contents of StringBuffer, * set length of StringBuffer to 0. */ sbf.setLength(0); System.out.println("StringBuffer contents deleted:" + sbf); } } /* Output of Java StringBuffer setLength example would be StringBuffer contents: StringBuffer StringBuffer contents deleted: */ |
thats great and very useful thanks a lot
Thanks, It’s very much use-ful.