Here some important method with example is as given bellow.
Example :
StringBuilder class in reverse() method ..
In reverse() method of StringBuilder class reverses the current string that is given..
class test{ public static void main(String args[]){ StringBuilder strb=new StringBuilder("java"); strb.reverse(); System.out.println(strb);//prints avaj }} |
Example:
StringBuilder class in delete() method..
In delete() method of StringBuilder class deletes the string from the
specified beginIndex to endIndex as given string.
class test{ public static void main(String args[]){ StringBuilder strb=new StringBuilder("Hello java"); strb.delete(1,7); System.out.println(strb);//prints Hva }} |
Example:
StringBuilder class in replace() method..
In replace() method replaces the given string from the specified beginIndex and endIndex as you want.
class test{ public static void main(String args[]){ StringBuilder strb=new StringBuilder("you in java"); strb.replace(3,5,"welcome"); System.out.println(strb);//prints you welcome in java }} |
Example:
StringBuilder class in append() method..
In append() method concatenates the given string with this string.(as given before)
class test{ public static void main(String args[]){ StringBuilder strb=new StringBuilder("welcome "); strb.append("Java");//now here original string is changed System.out.println(strb);//prints welcome Java }} |
Example:
StringBuilder class in insert() method..
In insert() method inserts the given string with this string at the given position as you want.
class test{ public static void main(String args[]){ StringBuilder strb=new StringBuilder("my"); strb.insert(2,"Java");//now original string is changed System.out.println(strb);//prints myjava }} |
Example:
StringBuilder class in capacity() method..
In capacity() method of StringBuilder class
returns the current capacity of the Builder. The by default capacity of the
Builder is 16.
If the number of character increases from its current capacity, it increases the
capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it
will be (16*2)+2=34.
class test{ public static void main(String args[]){ StringBuilder strb=new StringBuilder(); System.out.println(strb.capacity());//by default 16 strb.append("india"); System.out.println(strb.capacity());//now 16 strb.append("india is great"); System.out.println(strb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2 }} |
Read More →