Java StringBuilder append() method

append(String str): append the specified string at the end of this string. Syntax: public StringBuilder append(String str)   Note: if the specified string is null then it appends a “null” string at the end. If the append has an object, int, double, etc as an argument then the argument value is first converted into a … Read more

Java StringBuilder

StringBuilder is the same as StringBuffer except that StringBuilder is not synchronized, hence it is not thread-safe. i.e. StringBuilder class objects are non-thread-safe, mutable sequences of characters. Note: StringBuilder does not override the equals method of the object class, so StringBuilder objects should be converted into string objects if you want to compare StringBuilder class … Read more

Java StringTokenizer

StringTokenizer class breaks a string into tokens using a specified delimiter. Note: space is the default delimiter.   Constructors of StringTokenizer class: public StringTokenizer(String str): creates a string tokenizer for the specified string. By default, space is taken as a delimiter here. public StringTokenizer(String str, String delimiter): creates a string tokenizer for the specified string … Read more

Java StringBuffer ensureCapacity() Method

ensureCapacity(int minCapacity): Java StringBuffer ensureCapacity ensures that the capacity is at least equal to the specified minimum. Syntax: public void ensureCapacity(int minCapacity) Note: If the current capacity is greater than the argument there will be no change in the current capacity. If the current capacity is less than the argument there will be a change in the … Read more

Java StringBuffer capacity() Method

capacity(): Returns the current capacity of the string buffer. Capacity refers to the amount of available storage. Syntax: public int capacity()   Example: class TestStringBuffer{ StringBuffer sb = new StringBuffer(); public void capacityTest(){ //default capacity. System.out.println(sb.capacity()); sb.append(“Hello “); //current capacity 16. System.out.println(sb.capacity()); sb.append(“w3schools.blog”); //current capacity (16*2)+2=34 i.e (oldcapacity*2)+2. System.out.println(sb.capacity()); } } public class StringBufferCapacityExample { … Read more

Java StringBuffer reverse() method

reverse() replace the string buffer’s character sequence with the reverse character sequence. Syntax: public synchronized  StringBuffer reverse()   Example class TestStringBuffer{ StringBuffer sb = new StringBuffer(“www.w3schools.blog”); public void reverseTest(){ //replace the string buffer’s character //sequence by reverse character sequence. System.out.println(sb.reverse()); } } public class StringBufferReverseExample { public static void main(String args[]){ //creating TestStringBuffer object TestStringBuffer … Read more

Java StringBuffer delete() Method

delete(int startIndex, int endIndex) delete the substring of the string buffer from startIndex to endIndex-1. Syntax: public synchronized StringBuffer delete(int startIndex, int endIndex)   Note: startIndex should be between 0 and to length of the string or less than endIndex, if it is not StringIndexOutOfBoundsException will be thrown.   Example: class TestStringBuffer{ StringBuffer sb = … Read more

Java StringBuffer replace() Method

replace(int startIndex, int endIndex, String str) The replace the substring of the string buffer from startIndex to endIndex-1 with the specified string. Syntax: public synchronized  StringBuffer replace(int startIndex, int endIndex, String str)   Note: startIndex should be between 0 and to length of the string or less than endIndex, if it is not StringIndexOutOfBoundsException will … Read more

Java StringBuffer insert() Method

insert(int offset, String str) insert a specified string at the offset indicated position.   Syntax: public synchronized StringBuffer insert(int offset, String str)   Note: If the specified string is null then it inserts a “null” string at the offset indicated position. If the insert has an object, int, double, etc instead of a string then … Read more

Java StringBuffer append() Method

append(String str) append the specified string at the end of this string. Syntax: public synchronized StringBuffer append(String str)   Note: if the specified string is null then it appends the “null” string at the end. If the append has an object, int, double, etc as an argument then the argument value is first converted into … Read more