java.lang

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…

trim() String Example

java.lang.String class comes with trim() String method to remove both leading and trailing whitespaces in the string. After trimming, a new string is returned. The signature of trim() String is given as defined in Java API. public String trim(): Returns a new string after deleting both prefix and suffix whitespaces…

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…

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…

split() String Example

java.lang.String class comes with overloaded split() String method to split a string into tokens. Introduced in JDK 1.4, earlier done with SringTokenizerr. The signature of split() String Example is given as defined in Java API. public String[] split(String regex): Returns an array of string words after splitting the string into…

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…

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…

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.

Output screenshot on concat() String…

hashCode() String Example

java.lang.String class comes with many methods towards increasing performance. Think of comparing two strings for their equality, each having 1000 characters. Comparing each character in a for loop is a time cosuming process where performance is lost. Java comes with an idea. Each string is converted into an integer value…

endsWith() String Example

Java String class comes with many string utility methods with which string manipulation becomes easier. Read these methods and you will remember the laborious days of C/C++. One such method is endsWith() String. The present method endsWith(String str) checks the given string ends with a few characters like str or…

startsWith() String Example

The String class comes with many helper methods with which Java string manipulation becomes easier. Forget the laborious days of C/C++. One such utility method is startsWith() String. The present method startsWith(String str) checks the given string starts with a few characters like str or not. If starts with characters…

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);…

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()…

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…

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…

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…

getChars() String Example

java.lang.String class comes with many methods to manipulate string. The methods are a boon to Programmers without which would have been a laborious code like C/C++. Present method is getChars() String. getChars() copies the characters of a sting to a character array. Or, a string can be converted into a…

getBytes() String Example

Many methods exist in java.lang.String class used for different string manipulations like checking the availability of a character, to extract a part of a string or to convert a data type to string etc. The present method is on getBytes() String. getBytes() returns a byte array where elements are ASCII…

charAt() String Example

Many methods exist in java.lang.String class used for different string manipulations like checking the availability of a character, to extract a part of a string or to convert a data type to string etc. The present method is charAt() String. charAt(int index) extracts a single character from a string. Following…