class StringBuffer

StringBuffer Constructors Example

StringBuffer is mutable and String is immutable. To modify the string present in the buffer, many methods are defined in StringBuffer class. Apart methods, there are 4 types of StringBuffer Constructorsas on Java SE 7. public StringBuffer() public StringBuffer(int initialCapacity) public StringBuffer(String str) public StringBuffer(CharSequence seq) Let us see what Java API says about StringBuffer …

StringBuffer Constructors Example Read More »

reverse() StringBuffer Example

java.lang.StringBuffer class comes with many utility methods to manipulate string. One of the method is reverse() used to reverse the contents of the buffer. After this method call, original buffer itself gets effected. Note: reverse() is not a method of String; applying on string raises compilation error. What Java API says about reverse() StringBuffer: public …

reverse() StringBuffer Example Read More »

lastIndexOf() StringBuffer Example

java.lang.StringBuffer class comes with many helper methods to manipulate string. One of the methods is overloaded lastsIndexOf() method used to search for the last occurrence of a stirng in the buffer. What Java API says abou lastIndexOf() StringBuffer: public int lastsIndexOf(String searchString): Returns an int value of the last occurrence of the index number of …

lastIndexOf() StringBuffer Example Read More »

indexOf() StringBuffer Example

java.lang.StringBuffer class comes with many helper methods to manipulate string. One of the methods is overloaded indexOf() method used to search for the first occurrence of a string in the buffer. Let us see what Java API says about indexOf() StringBuffer: public int indexOf(String searchString): Returns an int value of the index number of first …

indexOf() StringBuffer Example Read More »

substring() StringBuffer Example

java.lang.StringBuffer class comes with many methods to manipulate string. One of the methods is overloaded substring() method used to extract part of the characters present in the buffer. What Java API says about substring() StringBuffer: public synchronized java.lang.String substring(int index1): Returns a string with the characteres of StringBuffer starting from index number index1 to the …

substring() StringBuffer Example Read More »

replace() StringBuffer Example

replace() StringBuffer Method java.lang.StringBuffer class comes with many methods to manipulate string. replace() method replaces a group of characters in the buffer. What Java API says about replace() StringBuffer: public synchronized java.lang.StringBuffer replace(int startIndex, int endIndex, String string1): Replaces a group of characters of string buffer from startIndex to endIndex-1 with string string1. Replaced number …

replace() StringBuffer Example Read More »

deleteCharAt() StringBuffer Example

To manipulate string, java.lang.StringBuffer class comes with many methods, otherwise which will take long laborious coding as in C/C++. One such method is deleteCharAt() StringBufferthat deletes a single character. What Java API says about deleteCharAt() StringBuffer: public synchronized java.lang.StringBuffer deleteCharAt(int index1): Deletes a single character from buffer at index number index1. Throws StringIndexOutOfBoundsException if the …

deleteCharAt() StringBuffer Example Read More »

delete() StringBuffer Example

Deleting a group of characters with delete() StringBuffer To manipulate string, java.lang.StringBuffer class comes with many methods, otherwise which will take long laborious coding as in C/C++. One such method is delete(int, int) that deletes a group of characters. What Java API says of delete() StringBuffer: public synchronized java.lang.StringBuffer delete(int startIndex, int endIndex): Deletes a …

delete() StringBuffer Example Read More »

setCharAt() StringBuffer Example

To manipulate string, java.lang.StringBuffer class comes with many methods, otherwise, which will take long laborious coding as in C/C++. One such method is setCharAt(int, char) that replaces a single character in the string buffer. Now read setCharAt() StringBuffer. What Java API says of setCharAt() StringBuffer: public synchronized void setCharAt(int index1, char1): Replaces the character in …

setCharAt() StringBuffer Example Read More »

getChars() StringBuffer Example

To convert StringBuffer contents to an array of characters with getChars() StringBuffer. Sometimes, it may be needed to copy a few characters of string content of StringBuffer to an array or to copy the complete StringBuffer into an array. Here, getChars() method comes into usage. What Java API says about getChars() StringBuffer: public synchronized void …

getChars() StringBuffer Example Read More »

ensureCapacity() vs setLength() vs trimToSize() StringBuffer

Before going into the table of ensureCapacity() vs setLength() vs trimToSize(), it is advised to refer the programs at: ensureCapacity() StringBuffer Example setLength() StringBuffer Example trimToSize() StringBuffer Example Following terminology is to be remembered: Length: It is the number of characters present in the buffer (which generally be same or less than the capacity). Capacity: …

ensureCapacity() vs setLength() vs trimToSize() StringBuffer Read More »

trimToSize() StringBuffer Example

When the coding comes to an end with string manipulation in the buffer and further no modification is required, keeping extra buffer is memory waste. To remove the extra buffer than required to hold the characters, Programmer calls this method. What Java API says: public synchronized void trimToSize(): Deletes the extra buffer existing more than …

trimToSize() StringBuffer Example Read More »

charAt() StringBuffer Example

To manipulate string, java.lang.StringBuffer class comes with many methods, otherwise which will take long laborious coding as in C/C++. One such method is charAt() StringBuffer that returns the character at the specified index number passed as argument to the method. What Java API says about charAt() StringBuffer: public synchronized char charAt(int index): Returns the character …

charAt() StringBuffer Example Read More »

setLength() StringBuffer Example

setLength() StringBuffer method sets the number of characters that can exist in the buffer. Must be careful in the usage: if the setLengtgh() is set with less size of number of characters, the extra characters in the buffer will be truncated off. What Java API says ablout setLength() StringBuffer: public synchronized void setLength(int newLength): sets …

setLength() StringBuffer Example Read More »

capacity() StringBuffer Example

StringBuffer comes with many utility methods to do operations on strings that do not exist in String class. One such method is capacity() StringBuffer. StringBuffer class comes with two methods – length() to find the number of characters present in the buffer and capacity() method to know the buffer size. We know StringBuffer comes with …

capacity() StringBuffer Example Read More »

length() StringBuffer Example

StringBuffer comes with many utility methods to do operations on strings apart String class own methods. One such method is length() StringBuffer. StringBuffer class comes with two methods – length() to find the number of characters present in the buffer and capacity() method to know the buffer size. Following is the length() StringBuffer method signature …

length() StringBuffer Example Read More »

reverse() StringBuffer Example

StringBuffer comes with many utility methods to do operations on strings apart String class own methods. One such method is reverse() method. StringBuffer, with its nature of mutability, comes with many methods that do not exist in String. One useful method is reverse() method which reverses the existing contents in the StringBuffer. Anywhere in Java, …

reverse() StringBuffer Example Read More »

append() StringBuffer Example

StringBuffer, with its nature of mutability, comes with many methods that do not exist in String. One useful method is append() StringBuffer method which appends anything to the existing contents. Following is the method signature of append() StringBuffer as defined in Java API. public StringBuffer append(String str): String str is appended at the end of …

append() StringBuffer Example Read More »

insert() StringBuffer Example

java.lang.StringBuffer class comes with many methods to manipulate string. One of such methods is insert() overloaded 12 times taking different parameters. insert() StringBuffer is used to insert some data in the buffer. Observe the 12 overloaded methods of insert() StringBuffer: public synchronized java.lang.StringBuffer insert(int, java.lang.Object); public synchronized java.lang.StringBuffer insert(int, java.lang.String); public synchronized java.lang.StringBuffer insert(int, char[]); …

insert() StringBuffer Example Read More »

ensureCapacity() StringBuffer Example

ensureCapacity() StringBuffer method gives guarantee the existing capacity. It behaves in two ways depending on the argument passed to the method. If the argument of the method is less than the existing capacity, then there will be no change in existing capacity (observe, sb1 in the example). If the argument is greater than the existing …

ensureCapacity() StringBuffer Example Read More »