Skip to content
Java

String API Reference

Java String methods — immutable character sequences.

By EZ4Code Team

String

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

int length()

Return the length of this string.

Returns: int

char charAt(int index)

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

Returns: char

String substring(int beginIndex, int endIndex)

Return a new string that is a substring of this string, from beginIndex to endIndex (exclusive).

Returns: String

int indexOf(String str)

Return the index of the first occurrence of str, or -1 if not found.

Returns: int

String[] split(String regex)

Split this string around matches of the given regular expression.

Returns: String[]

String trim()

Return a string with leading and trailing whitespace removed (code points <= U+0020).

Returns: String

String replace(CharSequence target, CharSequence replacement)

Return a new string with each substring matching target replaced by replacement.

Returns: String

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.

Returns: boolean

int compareTo(String anotherString)

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

Returns: int

String toUpperCase()

Convert all characters to upper case using the default locale.

Returns: String

String toLowerCase()

Convert all characters to lower case using the default locale.

Returns: String

boolean contains(CharSequence s)

Return true if and only if this string contains the specified sequence of char values.

Returns: boolean

More Java API References