Java String Methods
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.
Method | Return Type | Description |
length() | int | Returns the total number of characters present in the string. |
charAt() | char | At the given index, whatever the character is present gets returned. |
concat() | String | Adds a string at the end of another string |
equals() | boolean | Compares 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() | boolean | Compares 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() | boolean | In a given string, the method looks for a specified sequence of characters. If found true is returned; otherwise, false. |
isEmpty() | boolean | Checks 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() | int | Returns the index of the last occurrence of the characters specified in the given string. |
hashCode() | int | Hash code of the given string is returned by this method. |
endsWith() | Boolean | Checks 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() | String | Convert each and every character of the given into small case. |
toUpperCase() | String | Convert each and every character of the given into upper case. |
trim() | String | Removes white spaces from both ends of the given string. |
replaceAll() | String | Replaces each and every occurrence of the substring that is matching with the given string. |
replaceFirst() | String | Replaces the first occurrence of the substring that is matching with the given string. |
compareTo() | int | Compares two strings lexicographically. The compareTo() method takes case sensitivity into consideration. |
compareToIgnoreCase() | int | Compares two strings lexicographically. The compareToIgnoreCase () method does not take case sensitivity into consideration. |
indexOf() | int | Looks 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() | String | Extracts characters from the given string from the specified position and returns it. |
regionMatches() | boolean | Checks whether regions of the two given strings match or not. |
getChars() | void | Creates 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() | boolean | Looks for a string that matches the specified regular expression. |
intern() | String | Return the canonical representation of the specified string object. |
subsequence() | CharSequence | Return a new character sequence that is sequence of the specified sequence. |
codePointBefore() | int | Returns the Unicode of the character that is present just before the specified character. |
codePointAt() | int | Returns the Unicode of the character that is present at the specified index. |
valueOf() | String | The method converts any specified data type into the string and then returns it. |