S. Nageswara Rao, Corporate Trainer

S. N. Rao has 20+ years hands on experience in Java and J2EE technologies. He has written 20+ books on java for students pursuing graduate and PG studies. Apart writing books, he is busy in taking corporate trainings.

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 »

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. Reversing using reverse() method. public …

Java String reverse with and without reverse() Example Read More »

toCharArray() String Example

java.lang.String class comes with toCharArray() String method to convert string to a character array. The signature is given of toCharArray() String as defined in Java API. public char[] toCharArray(): Returns a character array comprising the string characters as elements. In the following toCharArray() String is used to get a char array out of string. public …

toCharArray() String Example Read More »

toUpperCase() String Example

java.lang.String class comes with toUpperCase() String method to convert all lowercase letters into uppercase in the string. After modification, a new string is returned with all the conversions. The signature toUpperCase() String is given as defined in Java API. public String toUpperCase(): Returns a new string after converting all the lowercase letters to uppercase letters …

toUpperCase() String Example Read More »

toLowerCase() String Example

java.lang.String class comes with toLowerCase() String method to convert all uppercase letters into lowercase in the string. After modification, a new string is returned with all the conversions. The signature of toLowerCase() String is given as defined in Java API. public String toLowerCase(): Returns a new string after converting all the uppercase letters to lowercase …

toLowerCase() String Example Read More »

replaceFirst() String Example

After seeing replace() method in String, let us study another similar method replaceFirst() String. What Java API says about the method: String replaceFirst (String str, String replaceString): Replaces the first occurrence of str in the original string(calling string) with replaceString, The original string is not getting disturbed, but the returned string gets effected. See example. …

replaceFirst() String Example Read More »

replace() String Example

java.lang.String class comes with replace() String method to replace existing characters in a sting with new characters. The signature of replace() String as given in Java API. public String replace(char oldChar, char newChar): Returns a new string after replacing characters. All the occurrences of oldChar in the string are replaced by newChar. In the following …

replace() String Example Read More »

concat() String Example

java.lang.String class comes with concat() String method to concatenate two strings. The signature is given as defined in String class. public String concat(String str): Returns a new string, the result of concatenation of two strings. Following example concat() String illustrates the usage concat() method. public class StringMethodDemo { public static void main(String args[]) { String …

concat() String 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 »

regionMatches() String Example

regionMatches() used to compare substrings of two strings java.lang.String class comes with many methods to check two strings contain same characters in the same sequence. To do this, String class comes with equals(), equalsIgnoreCase(), compareTo() and compareToIgnoreCase() methods. All the above comparisons are used to compare whole strings. But regionMatches() compares parts of two strings …

regionMatches() String Example Read More »

compareToIgnoreCase() String Example

java.lang.String class comes with many methods to check two strings contain same characters in the same sequence. To do this, String class comes with equals(), compareTo(), equalsIgnoreCase() and compareToIgnoreCase() methods. This is tutorial is on compareToIgnoreCase() String. We have seen earlier compareTo() method which does case-sensitive comparison lexicographically. Now we discuss compareToIgnoreCase() method which does …

compareToIgnoreCase() String Example Read More »

compareTo() String Example

java.lang.String class comes with many methods to check two strings contain same characters in the same sequence. To do this, String class comes with equals(), compareTo(), equalsIgnoreCase() and compareToIgnoreCase() methods. This tutorial is on compareTo() String. Now we discuss compareTo() String method. Following is the method signature as defined in String class. public int compareTo(String …

compareTo() String Example Read More »

contentEquals() String Example

We know earlier equals() method is used to compare two strings whether they have same sequence of characters or not. But this method cannot compare String with StringBuffer. Even if they have same characters, the equals() returns false. In this case use contentEquals() String. contentEquals() String method is useful to compare String with StringBuffer or …

contentEquals() String Example Read More »