String
12 methodsThe String class represents character strings. All string literals are instances of String.
int length()Return the length of this string.
Returns
int
Example
"hello".length() // 5
"".length() // 0char charAt(int index)Return the char value at the specified index. Throws StringIndexOutOfBoundsException.
Parameters
| Name | Type | Description |
|---|---|---|
| index | int | 0-based index. |
Returns
char
Example
"hello".charAt(1) // 'e'
"hello".charAt(0) // 'h'String substring(int beginIndex, int endIndex)Return a new string that is a substring of this string, from beginIndex to endIndex (exclusive).
Parameters
| Name | Type | Description |
|---|---|---|
| beginIndex | int | Start (inclusive). |
| endIndex | int | End (exclusive). |
Returns
String
Example
"hello".substring(1, 3) // "el"
"hello".substring(2) // "llo"int indexOf(String str)Return the index of the first occurrence of str, or -1 if not found.
Parameters
| Name | Type | Description |
|---|---|---|
| str | String | Substring to find. |
Returns
int
Example
"hello".indexOf("ll") // 2
"hello".indexOf("z") // -1String[] split(String regex)Split this string around matches of the given regular expression.
Parameters
| Name | Type | Description |
|---|---|---|
| regex | String | Delimiting regex. |
Returns
String[]
Example
"a,b,c".split(",") // ["a", "b", "c"]
"hello".split("") // ["h","e","l","l","o"]
"a-b--c".split("-") // ["a", "b", "", "c"]String trim()Return a string with leading and trailing whitespace removed (code points <= U+0020).
Returns
String
Example
" hi ".trim() // "hi"
"\n hello\n".trim() // "hello"String replace(CharSequence target, CharSequence replacement)Return a new string with each substring matching target replaced by replacement.
Parameters
| Name | Type | Description |
|---|---|---|
| target | CharSequence | Literal sequence to replace. |
| replacement | CharSequence | Replacement. |
Returns
String
Example
"a-b-c".replace("-", "_") // "a_b_c"
"hello".replace("l", "L") // "heLLo"boolean equals(Object anObject)Compare this string to the specified object. True if and only if the argument is a String representing the same sequence of characters.
Parameters
| Name | Type | Description |
|---|---|---|
| anObject | Object | Object to compare. |
Returns
boolean
Example
"hello".equals("hello") // true
"hello".equals("Hello") // false (case-sensitive)
new String("x") == "x" // false (reference compare)
new String("x").equals("x") // trueint compareTo(String anotherString)Compare two strings lexicographically. Returns 0 if equal, a negative int if this is less, positive if greater.
Parameters
| Name | Type | Description |
|---|---|---|
| anotherString | String | String to compare. |
Returns
int
Example
"abc".compareTo("abd") // negative
"abc".compareTo("abc") // 0
"abd".compareTo("abc") // positiveString toUpperCase()Convert all characters to upper case using the default locale.
Returns
String
Example
"hello".toUpperCase() // "HELLO"
"café".toUpperCase() // "CAFÉ"String toLowerCase()Convert all characters to lower case using the default locale.
Returns
String
Example
"HELLO".toLowerCase() // "hello"
"CAFÉ".toLowerCase() // "café"boolean contains(CharSequence s)Return true if and only if this string contains the specified sequence of char values.
Parameters
| Name | Type | Description |
|---|---|---|
| s | CharSequence | Sequence to search for. |
Returns
boolean
Example
"hello world".contains("world") // true
"hello".contains("z") // false