String class in Java

The String class in Java is a built-in class that provides methods for manipulating strings. In Java, strings are treated as objects, and the String class provides a wide range of methods for working with strings. Some of the most commonly used methods of the String class include:


  1. 1. length() - returns the number of characters in the string.


  2. 2. charAt(int index) - returns the character at the specified index.


  3. 3. substring(int beginIndex, int endIndex) - returns a new string that is a substring of this string.


  4. 4. indexOf(String str) - returns the index within this string of the first occurrence of the specified substring.


  5. 5. replace(char oldChar, char newChar) - returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.


  6. 6. toLowerCase() and toUpperCase() - returns a new string with all the characters in lowercase or uppercase, respectively.


  7. 7. split(String regex) - splits this string around matches of the given regular expression


  8. 8. concat(String str) - concatenates the specified string to the end of this string.


The String class is also immutable, meaning that once a string object is created, its value cannot be changed. If you want to modify a string, you need to create a new string object with the desired modifications.


In addition to these methods, the String class also provides several constructors that allow you to create a string from an array of characters, from a string buffer, or from other string objects.