class String

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 equalsIgnoreCase() Using lexicographical order, compareTo() …

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

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 »