Java StringBuilder reverse() method

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

Java StringBuilder delete() method

delete(int startIndex, int endIndex): delete the substring of the string builder from startIndex to endIndex-1. Syntax: public StringBuilder 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.   StringBuilder delete() Example class TestStringBuilder{ StringBuilder sb … Read more

Java StringBuilder replace() method

replace(int startIndex, int endIndex, String str): replace the substring of the string builder from startIndex to endIndex-1 with the specified string.   Syntax: public StringBuilder 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 be … Read more

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