The String
class in Java provides a number of methods for manipulating strings. Here are some of the most commonly used methods:
length()
: Returns the length of the string (the number of characters it contains).
charAt(int index)
: Returns the character at the specified index in the string. The first character has index 0.
indexOf(char c)
: Returns the index of the first occurrence of the specified character in the string, or -1 if the character is not found.
substring(int beginIndex, int endIndex)
: Returns a new string that is a substring of the original string, starting at the specified beginIndex
and ending at the specified endIndex - 1
.
toLowerCase()
: Returns a new string with all the characters in the original string converted to lowercase.
toUpperCase()
: Returns a new string with all the characters in the original string converted to uppercase.
trim()
: Returns a new string with leading and trailing white space removed.
replace(char oldChar, char newChar)
: Returns a new string with all occurrences of the specified oldChar
replaced with the specified newChar
.
split(String regex)
: Splits the string into an array of substrings by dividing it at each occurrence of the specified regular expression (regex).
You can find more information about these and other methods of the String
class in the Java documentation.