The String class in Java provides many methods that can be used to manipulate and work with strings. Here is a brief overview of some of the most commonly used methods:
charAt(int index): Returns the character at the specified index in the string.
length(): Returns the length of the string.
substring(int beginIndex): Returns a substring of the original string, starting from the specified index.
substring(int beginIndex, int endIndex): Returns a substring of the original string, starting from the specified begin index and ending at the specified end index.
toLowerCase(): Returns a new string with all the characters converted to lowercase.
toUpperCase(): Returns a new string with all the characters converted to uppercase.
equals(Object obj): Compares the string to the specified object for equality.
equalsIgnoreCase(String str): Compares the string to another string, ignoring case differences.
indexOf(int ch): Returns the index of the first occurrence of the specified character in the string.
indexOf(int ch, int fromIndex): Returns the index of the first occurrence of the specified character, starting from the specified index.
indexOf(String str): Returns the index of the first occurrence of the specified substring in the string.
indexOf(String str, int fromIndex): Returns the index of the first occurrence of the specified substring, starting from the specified index.
replace(char oldChar, char newChar): Returns a new string with all occurrences of the specified character replaced with another character.
replace(charSequence target, charSequence replacement): Returns a new string with all occurrences of the specified substring replaced with another substring.
split(String regex): Splits the string into an array of substrings, using the specified regular expression as the delimiter.
trim(): Returns a new string with leading and trailing white space removed.
These are just a few of the many methods available in the String class in Java. Other methods include startsWith, endsWith, concat, compareTo, and many more.
Here's an example program in Java that demonstrates some of the methods available in the String class:
public class StringMethodsExample {
public static void main(String[] args) {
String str = "Hello, World!";
// charAt method
char firstChar = str.charAt(0);
System.out.println("First character: " + firstChar);
// length method
int strLength = str.length();
System.out.println("String length: " + strLength);
// substring method
String subStr = str.substring(7);
System.out.println("Substring from index 7: " + subStr);
String subStr2 = str.substring(7, 12);
System.out.println("Substring from index 7 to 12: " + subStr2);
// toLowerCase method
String lowerCaseStr = str.toLowerCase();
System.out.println("Lowercase string: " + lowerCaseStr);
// toUpperCase method
String upperCaseStr = str.toUpperCase();
System.out.println("Uppercase string: " + upperCaseStr);
// equals method
boolean isEqual = str.equals("Hello, World!");
System.out.println("Is string equal to 'Hello, World!': " + isEqual);
// equalsIgnoreCase method
boolean isEqualIgnoreCase = str.equalsIgnoreCase("hello, world!");
System.out.println("Is string equal to 'hello, world!': " + isEqualIgnoreCase);
// indexOf method
int index = str.indexOf('W');
System.out.println("Index of 'W': " + index);
int index2 = str.indexOf("World");
System.out.println("Index of 'World': " + index2);
// replace method
String replacedStr = str.replace('o', 'x');
System.out.println("String with 'o' replaced with 'x': " + replacedStr);
String replacedStr2 = str.replace("World", "Universe");
System.out.println("String with 'World' replaced with 'Universe': " + replacedStr2);
// split method
String[] splitStr = str.split(", ");
System.out.println("Split string: ");
for (String s : splitStr) {
System.out.println(s);
}
// trim method
String trimmedStr = " Hello, World! ".trim();
System.out.println("Trimmed string: " + trimmedStr);
}
}