Substring Removal from a String. Use String Buffer Class.

Question 1:

Substring Removal from a String. Use String Buffer Class.

Code:

public class Test {

   public static void main (String args[])
   {
       String str="Substring removal test";
       StringBuffer strbuf= new StringBuffer(str);
       System.out.println("The string before removal:"+strbuf);
       System.out.println("Removing text in the above String . . .");
       strbuf.delete(strbuf.indexOf("test"), strbuf.length());
       System.out.println("The string after removal:"+strbuf);
   }
}

Output:

The string before removal:Substring removal test
Removing text in the above String . . .
The string after removal:Substring removal

No comments:

Post a Comment