public class StringMethodsDemo {
public static void main(String[] args) {
String str = "Hello, World!";
// length()
int length = str.length();
System.out.println("Length: " + length);
// 输出:Length: 13
// charAt(int index)
char ch = str.charAt(7);
System.out.println("Character at index 7: " + ch);
// 输出:Character at index 7: W
// indexOf(String str)
int index = str.indexOf("World");
System.out.println("Index of 'World': " + index);
// 输出:Index of 'World': 7
// lastIndexOf(String str)
int lastIndex = str.lastIndexOf("o");
System.out.println("Last index of 'o': " + lastIndex);
// 输出:Last index of 'o': 8
// startsWith(String prefix)
boolean startsWith = str.startsWith("Hello");
System.out.println("Starts with 'Hello': " + startsWith);
// 输出:Starts with 'Hello': true
// endsWith(String suffix)
boolean endsWith = str.endsWith("World");
System.out.println("Ends with 'World': " + endsWith);
// 输出:Ends with 'World': false
// isEmpty()
boolean isEmpty = str.isEmpty();
System.out.println("Is empty: " + isEmpty);
// 输出:Is empty: false
// substring(int beginIndex)
String substring1 = str.substring(7);
System.out.println("Substring from index 7: " + substring1);
// 输出:Substring from index 7: World!
// substring(int beginIndex, int endIndex)
String substring2 = str.substring(7, 12);
System.out.println("Substring from index 7 to 12: " + substring2);
// 输出:Substring from index 7 to 12: World
// replace(char oldChar, char newChar)
String replaced = str.replace('o', 'O');
System.out.println("Replaced string: " + replaced); // 输出:Replaced string: HellO, WOrld!
// replace(CharSequence target, CharSequence replacement)
String replaced2 = str.replace("World", "Universe");
System.out.println("Replaced string: " + replaced2);
// 输出:Replaced string: Hello, Universe!
// split(String regex)
String[] splitArray = str.split(",");
System.out.println("Split array: " + Arrays.toString(splitArray));
// 输出:Split array: [Hello, World!]
// trim()
String trimmed = str.trim();
System.out.println("Trimmed string: " + trimmed);
// 输出:Trimmed string: Hello, World!
// equals(Object obj)
boolean equals = str.equals("Hello, World!");
System.out.println("Equals 'Hello, World!': " + equals);
// 输出:Equals 'Hello, World!': true
// equalsIgnoreCase(String anotherString)
boolean equalsIgnoreCase = str.equalsIgnoreCase("hello, world!");
System.out.println("Equals ignore case 'hello, world!': " + equalsIgnoreCase);
// 输出:Equals ignore case 'hello, world!': true
// toLowerCase()
String lowerCase = str.toLowerCase();
System.out.println("Lower case: " + lowerCase);
// 输出:Lower case: hello, world!
// toUpperCase()
String upperCase = str.toUpperCase();
System.out.println("Upper case: " + upperCase); // 输出:Upper case: HELLO, WORLD!
// concat(String str)
String concat = str.concat(" How are you?");
System.out.println("Concatenated string: " + concat);
// 输出:Concatenated string: Hello, World! How are you?
// contains(CharSequence sequence)
boolean contains = str.contains("World");
System.out.println("Contains 'World': " + contains);
// 输出:Contains 'World': true
// format(String format, Object... args)
String formatted = String.format("The value of pi is approximately %.2f", Math.PI);
System.out.println("Formatted string: " + formatted);
// 输出:Formatted string: The value of pi is approximately 3.14
}
}