Method |
Description |
Example |
length() |
Returns the length of the string. |
"Hello".length() returns 5 |
charAt(int index) |
Returns the character at the specified index. |
"Hello".charAt(0) returns 'H' |
isEmpty() |
Returns true if the string is empty. |
"".isEmpty() returns true |
toUpperCase() |
Converts the string to uppercase. |
"hello".toUpperCase() returns "HELLO" |
toLowerCase() |
Converts the string to lowercase. |
"HELLO".toLowerCase() returns "hello" |
equals(Object obj) |
Compares the string to the specified object. |
"hello".equals("world") returns false |
equalsIgnoreCase(String anotherString) |
Compares the string to another string, ignoring case. |
"hello".equalsIgnoreCase("HELLO") returns true |
compareTo(String anotherString) |
Compares two strings lexicographically. |
"abc".compareTo("def") returns a negative integer |
compareToIgnoreCase(String str) |
Compares two strings lexicographically, ignoring case. |
"ABC".compareToIgnoreCase("abc") returns 0 |
startsWith(String prefix) |
Tests if the string starts with the specified prefix. |
"hello".startsWith("he") returns true |
endsWith(String suffix) |
Tests if the string ends with the specified suffix. |
"hello".endsWith("lo") returns true |
contains(CharSequence s) |
Returns true if the string contains the specified sequence of characters. |
"hello".contains("ell") returns true |
indexOf(int ch) |
Returns the index within the string of the first occurrence of the specified character. |
"hello".indexOf('l') returns 2 |
lastIndexOf(int ch) |
Returns the index within the string of the last occurrence of the specified character. |
"hello".lastIndexOf('l') returns 3 |
substring(int beginIndex) |
Returns a substring of the string, starting at the specified index. |
"hello".substring(2) returns "llo" |
substring(int beginIndex, int endIndex) |
Returns a substring of the string, starting at the specified begin index and ending before the specified end index. |
"hello".substring(1, 4) returns "ell" |
replace(char oldChar, char newChar) |
Returns a new string resulting from replacing all occurrences of the specified character with another character. |
"hello".replace('l', 'L') returns "heLLo" |
replace(CharSequence target, CharSequence replacement) |
Returns a new string resulting from replacing all occurrences of the specified target sequence with another sequence. |
"hello".replace("l", "L") returns "heLLo" |
replaceAll(String regex, String replacement) |
Replaces each substring of the string that matches the given regular expression with the given replacement. |
"hello".replaceAll("l", "L") returns "heLLo" |
split(String regex) |
Splits the string around matches of the given regular expression. |
"hello world".split(" ") returns ["hello", "world"] |
trim() |
Returns a string whose value is this string, with leading and trailing whitespace removed. |
" hello ".trim() returns "hello" |