The String class in Java is one of the most commonly used classes in the Java programming language. It represents a sequence of characters and provides a variety of methods to manipulate and handle 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 strings in Java. These methods are essential for efficient coding practices and help in performing operations like finding characters, comparing strings, and modifying string content.
For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java Programming Tutorial.
String Class Methods
| Method | Description |
|---|---|
| valueOf() | Returns the string representation of the given argument. |
| charAt() | Returns the character at the specified index. |
| concat() | Concatenates the specified string to the end of this string. |
| contains() | Checks if the string contains the specified sequence of char values. |
| endsWith() | Checks if the string ends with the specified suffix. |
| equals() | Compares this string to the specified object. |
| getBytes() | Encodes this string into a sequence of bytes using the specified charset. |
| indexOf() | Returns the index of the first occurrence of the specified character. |
| isEmpty() | Checks if the string is empty. |
| lastIndexOf() | Returns the index of the last occurrence of the specified character. |
| replace() | Returns a new string resulting from replacing all occurrences of old characters with new characters. |
| split() | Splits this string around matches of the given regular expression. |
| startsWith() | Checks if the string starts with the specified prefix. |
| substring() | Returns a new string that is a substring of this string. |
| toLowerCase() | Converts all of the characters in this string to lower case. |
| 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. |
| toCharArray() | Converts this string to a new character array. |
| replaceFirst() | Replaces the first substring of this string that matches the given regular expression with the given replacement. |
| 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. |
| isBlank() | Returns true if the string is empty or contains only white space codepoints. |
| 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 specified number of times. |
| indent() | Adjusts the indentation of each line of this string based on the specified integer. |
| transform() | Applies the provided function to this string and returns the result. |
| resolveConstantDesc() | Returns a nominal descriptor for the string, which can be used to reconstitute the string in a constant pool. |
| 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 each 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. |
References: