Java String Methods

Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java string methods that make our task easier.  The following table describes the String class methods.

MethodReturn TypeDescription
length()intReturns the total number of characters present in the string.
charAt()charAt the given index, whatever the character is present gets returned.
concat()StringAdds a string at the end of another string
equals()booleanCompares two string and returns true when there is a match otherwise return false. The equals() method considers the case – sensitivity of characters present in the strings.
equalsIgnoreCase()booleanCompares two string and returns true when there is a match otherwise, returns false. The equalsIgnoreCase() method does not consider the case – sensitivity of characters present in the strings.
contains()booleanIn a given string, the method looks for a specified sequence of characters. If found true is returned; otherwise, false.
isEmpty()booleanChecks whether a given string is empty or not. If the given string has no characters, i.e., found empty, true is returned; otherwise, false.
lastIndexOf()intReturns the index of the last occurrence of the characters specified in the given string.
hashCode()intHash code of the given string is returned by this method.
endsWith()BooleanChecks whether the given string ends with the specified characters or not
split()String[]The split() method breaks the given string into the array of smaller substrings.
toLowerCase()StringConvert each and every character of the given into small case.
toUpperCase()StringConvert each and every character of the given into upper case.
trim()StringRemoves white spaces from both ends of the given string.
replaceAll()StringReplaces each and every occurrence of the substring that is matching with the given string.
replaceFirst()StringReplaces the first occurrence of the substring that is matching with the given string.
compareTo()intCompares two strings lexicographically. The compareTo() method takes case sensitivity into consideration.
compareToIgnoreCase()intCompares two strings lexicographically. The compareToIgnoreCase () method does not take case sensitivity into consideration.
indexOf()intLooks for the occurrence of the first position of the given character in the specified string.
toCharArray()char[]Converts the specified string into the character array.
substring()StringExtracts characters from the given string from the specified position and returns it.
regionMatches()booleanChecks whether regions of the two given strings match or not.
getChars()voidCreates a copy of characters from a string to the specified array of characters.
getBytes()byte[]Encodes the specified string into a stream of bytes.
matches()booleanLooks for a string that matches the specified regular expression. 
intern()StringReturn the canonical representation of the specified string object.
subsequence()CharSequenceReturn a new character sequence that is sequence of the specified sequence.
codePointBefore()intReturns the Unicode of the character that is present just before the specified character.
codePointAt()intReturns the Unicode of the character that is present at the specified index.
valueOf()StringThe method converts any specified data type into the string and then returns it.

Pin It on Pinterest

Share This