String methods in Java
There are numerous string methods in java which are helpful for performing different and useful operations on Strings. Here is the table containing all the methods present in Strings. You can click on the methods name to know more details about it.
Sr No. | Methods Name | Description |
---|---|---|
1 | boolean equals(Object anObject) | It compares one String with another string object. If matches returns true, else false. |
2 | indexOf(String str, int fromIndex) | Returns the position within this string of the first occurrence of the specified substring ,starting from specified index. |
3 | int length() | It will return the length of this string. |
4 | String substring(int beginIndex) | Returns a new string that is a substring of this string.Substring start from the specified index |
5 | String substring(int beginIndex, int endIndex) |
Returns a new string that is a substring of this string.Substring start from the specified beginindex upto endindex-1. |
6 | String trim() | Returns a new string after removing leading and trailing whitespaces. |
7 | char charAt(int index) | Returns the char value at the specified index.Index start from 0. |
8 | int compareTo(String anotherString) | Compare 2 strings. The comparison is based on the Unicode value of each character in the strings. |
9 | int compareToIgnoreCase(String str) | Compare 2 strings. Same as compareTo above, it just ignore cases. |
10 | String concat(String str) | Appends the specified string to the end of this string. |
11 | boolean contains(CharSequence s) | Returns true if and only if this string contains the specified sequence of char values. |
12 | booleancontentEquals(CharSequence cs) | Compares this string to the specified CharSequence.Returns true if matches, else false. |
13 | boolean contentEquals(StringBuffer sb) | Compares this string to the specified StringBuffer.Returns true if matches, else false. |
14 | boolean endsWith(String suffix) | Tests if this string ends with the specified suffix. |
15 | boolean equalsIgnoreCase(String anotherString) | Compares this String to another String, ignoring case considerations. |
16 | byte[] getBytes() | Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array. |
17 | byte[] getBytes(Charset charset) | Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array. |
18 | void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin) | It copies the characters of src array to the dest array. Only the specified range is being copied(srcBegin to srcEnd) to the dest subarray(starting fromdestBegin). |
19 | int indexOf(String str) | Returns the index within this string of the first occurrence of the specified substring. |
20 | int indexOf(int ch) | Returns the index within this string of the first occurrence of the specified character. |
21 | int indexOf(int ch,int fromIndex) | Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. |
22 | boolean isEmpty() | Returns true if, and only if, length() is 0. |
23 | int lastIndexOf(int ch) | Returns the index within this string of the last occurrence of the specified character. |
24 | int lastIndexOf(String str) | Returns the index within this string of the last occurrence of the specified substring. |
25 | boolean matches(String regex) | It checks whether the String is matching with the specified regular expression regex. |
26 | boolean regionMatches(boolean ignoreCase, int srcoffset, String dest, int destoffset, int len) | Another variation of regionMatches method with the extra boolean argument to specify whether the comparison is case sensitive or case insensitive. |
27 | String replaceAll(String regex, String replacement) | It replaces all the occurrences of substrings that fits the regular expression regex with the replacement string. |
28 | String replaceFirst(String regex, String replacement) | Replaces the first substring of this string that matches the given regular expression with the given replacement. |
29 | String replace(char oldChar,char newChar) | Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. |
30 | String[] split(String regex) | Splits this string on the basis of matches of the given regular expression. |
31 | String[] split(String regex, int limit) | It splits the string and returns the array of substrings that matches the given regular expression.Limits decides the length of the resulting array. |
32 | boolean startsWith(String prefix) | Validates if this string starts with the specified prefix. |
33 | boolean startsWith(String prefix,int toffset) | Validates if this string starts with the specified prefix from the specified index. |
34 | char[] toCharArray() | Converts this string to a new character array. |
35 | String toLowerCase() | Converts this string to lower case. |
36 | String toUpperCase() | Converts this string to upper cases. |
37 | String toString() | This object (which is already a string!) is itself returned. |
Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.