Java String Methods

The String class in Java is one of the most widely used classes. It provides a variety of methods for manipulating strings, such as comparing, searching, and modifying strings. Understanding these methods is crucial for efficient string manipulation in Java.

This guide covers various methods available in the String class, offering a comprehensive understanding of how to manipulate and interact with string values in Java. These methods are essential for efficient coding practices and help in performing operations like parsing, comparing, and converting string values.

For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java String API Guide.

Method Description
valueOf() Returns the string representation of the specified value.
charAt() Returns the character at the specified index.
concat() Concatenates the specified string to the end of this string.
endsWith() Checks if this string ends with the specified suffix.
equals() Compares this string to the specified object.
getBytes() Encodes this string into a sequence of bytes.
indexOf() Returns the index within this string of the first occurrence of the specified character.
isEmpty() Checks if this string is empty.
lastIndexOf() Returns the index within this string of the last occurrence of the specified character.
replace() Replaces each occurrence of a specified character with a new character.
split() Splits this string around matches of the given regular expression.
startsWith() Checks if this string starts with the specified prefix.
substring() Returns a new string that is a substring of this string.
toLowerCase() Converts all characters in this string to lower case.
trim() Removes leading and trailing whitespace from this string.
codePointAt() Returns the Unicode code point at the specified index.
codePointBefore() Returns the Unicode code point before the specified index.
compareToIgnoreCase() Compares two strings lexicographically, ignoring case differences.
contentEquals() Compares this string to the specified CharSequence.
regionMatches() Tests if two string regions are equal.
replaceAll() Replaces each substring of this string that matches the given regular expression with the given replacement.
replaceFirst() Replaces the first substring of this string that matches the given regular expression with the given replacement.
subSequence() Returns a new character sequence that is a subsequence of this sequence.
toCharArray() Converts this string to a new character array.
strip() Returns a string whose value is this string, with all leading and trailing white space removed.
stripLeading() Returns a string whose value is this string, with all leading white space removed.
stripTrailing() Returns a string whose value is this string, with all trailing white space removed.
isBlank() Returns true if the string is empty or contains only white space codepoints, otherwise false.
lines() Returns a stream of lines extracted from this string, separated by line terminators.
repeat() Returns a string whose value is the concatenation of this string repeated the given number of times.
indent() Adjusts the indentation of each line of this string based on the value of n, and normalizes newlines.
transform() Applies this string as input to the given function and returns the function's result.
describeConstable() Returns an Optional describing the nominal descriptor for this instance, if one can be constructed, or an empty Optional if one cannot be.
resolveConstantDesc() Returns the nominal descriptor for this instance, which must be resolved to produce the constant value.
formatted() Returns a formatted string using the specified format string and arguments.
stripIndent() Returns a string whose value is this string, with incidental white space removed from every line.
copyValueOf() Returns a string that represents the character sequence in the array specified.
intern() Returns a canonical representation for the string object.
join() Returns a new string composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
matches() Tests if this string matches the given regular expression.
offsetByCodePoints() Returns the index within this string that is offset from the given index by codePointOffset code points.

Comments