java.lang

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…

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…

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…

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…

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…

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…

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…

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…

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,…

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…

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…

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…

equals() equalsIgnoreCase() String Example

Comparing Strings with equals() equalsIgnoreCase() String. First and foremost important operation on strings is to compare whether two strings are same or not. There are three styles which the programmer should be well aware of when to use what. The styles are Using logical equals to (==) Using equals() and…

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…

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…

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.…

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…

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…

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…

Java String reverse with and without reverse() Example

To reverse a string with less code in Java, there comes reverse() method in StringBuffer class. reverse() method cannot be used directly on string as reverse() is not defined in String class but defined in StringBuffer and StringBuilder. String should be converted to StringBuffer and then applied reverse() method. 1.…