Skip to content

Java String API

Java String methods — immutable character sequences.

1 class · 12 methods

String

12 methods

The String class represents character strings. All string literals are instances of String.

int length()

Return the length of this string.

Returns

int

Example

java
"hello".length()   // 5
"".length()         // 0
char charAt(int index)

Return the char value at the specified index. Throws StringIndexOutOfBoundsException.

Parameters

NameTypeDescription
indexint0-based index.

Returns

char

Example

java
"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

NameTypeDescription
beginIndexintStart (inclusive).
endIndexintEnd (exclusive).

Returns

String

Example

java
"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

NameTypeDescription
strStringSubstring to find.

Returns

int

Example

java
"hello".indexOf("ll")  // 2
"hello".indexOf("z")    // -1
String[] split(String regex)

Split this string around matches of the given regular expression.

Parameters

NameTypeDescription
regexStringDelimiting regex.

Returns

String[]

Example

java
"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

java
"  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

NameTypeDescription
targetCharSequenceLiteral sequence to replace.
replacementCharSequenceReplacement.

Returns

String

Example

java
"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

NameTypeDescription
anObjectObjectObject to compare.

Returns

boolean

Example

java
"hello".equals("hello")  // true
"hello".equals("Hello")  // false (case-sensitive)
new String("x") == "x"   // false (reference compare)
new String("x").equals("x")  // true
int compareTo(String anotherString)

Compare two strings lexicographically. Returns 0 if equal, a negative int if this is less, positive if greater.

Parameters

NameTypeDescription
anotherStringStringString to compare.

Returns

int

Example

java
"abc".compareTo("abd")   // negative
"abc".compareTo("abc")   // 0
"abd".compareTo("abc")   // positive
String toUpperCase()

Convert all characters to upper case using the default locale.

Returns

String

Example

java
"hello".toUpperCase()  // "HELLO"
"café".toUpperCase()    // "CAFÉ"
String toLowerCase()

Convert all characters to lower case using the default locale.

Returns

String

Example

java
"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

NameTypeDescription
sCharSequenceSequence to search for.

Returns

boolean

Example

java
"hello world".contains("world")  // true
"hello".contains("z")             // false