Problem : Describe java.lang.StringBuffer class methods
Program :Step 1 - CodingCreate a text file c:\sunilos\TestStringBuffer.java and copy below contents./* * A program implements String Buffer. */ public class TestStringBuffer { public static void main(String[] args) { StringBuffer sb = new StringBuffer("Vijay"); // Buffer is intialized by string "Vijay" sb.append(" Dinanath"); // Now append string " Dinanath" in string buffer sb.append(" Chouhan"); // Now append string " Chouhan" in string buffer String name = sb.toString(); // Convert StringBuffer into String object; System.out.println(" Buffer Length- " + sb.length()); // Find length of buffer System.out.println(" Buffer Capacity- " + sb.capacity()); // Find capacity of buffer System.out.println(" Character at position 6 - " + sb.charAt(6));// Find charAt given position System.out.println(" Dina index is- " + sb.indexOf("Dina")); // Find index of buffer System.out.println(" Last i Position- " + sb.lastIndexOf("i")); // Find last i index of buffer System.out.println(" subString is- " + sb.substring(13)); // Find subString of buffer System.out.println(" subString is start at 6 and ends at 13- " + sb.substring(6, 13)); System.out.println(" reverse the string - " + sb.reverse()); // reverse the string of buffer System.out.println(" 6 to 13 position is replaced by aaaaa- " + sb.replace(6, 13, "aaaaa")); System.out.println(" Delete at position at 6 to 12 is- " + sb.delete(6, 12)); System.out.println(" Delete char at 8 postion is - " + sb.deleteCharAt(8)); } } Step 2 - Deployment
Step 3 - Testing
OutputBuffer Length- 20Buffer Capacity- 21 Character at position 6 - D Dina index is- 6 Last i Position- 7 subString is- Chohan subString is start at 6 and ends at 13- Dinanth reverse the string - nahohC htnaniD yajiV 6 to 13 position is replaced by aaaaa- nahohCaaaaaD yajiV Delete at position at 6 to 12 is- nahohC yajiV Delete char at 8 postion is - nahohC yjiV <<Previous | Next>> |