java string
时间: 2023-10-06 13:08:44 浏览: 83
Java String is a sequence of characters that is used to represent text. It is one of the most commonly used classes in Java and is a part of the java.lang package. Strings are immutable in Java, which means that once a string object is created, its value cannot be changed.
Some common methods of the String class include:
1. length() - returns the length of the string.
2. charAt(int index) - returns the character at the specified index.
3. substring(int beginIndex, int endIndex) - returns a substring of the original string.
4. equals(String anotherString) - compares two strings for equality.
5. compareTo(String anotherString) - compares two strings lexicographically.
6. indexOf(String str) - returns the index of the first occurrence of the specified substring.
7. replace(char oldChar, char newChar) - replaces all occurrences of the old character with the new character.
8. toUpperCase() - converts the string to uppercase.
9. toLowerCase() - converts the string to lowercase.
Strings can be created using string literals or by creating objects of the String class. For example:
String str1 = "Hello World"; // using string literal
String str2 = new String("Hello World"); // using String object
阅读全文