揭秘String字符串方法的魅力之旅
2023-11-27 10:17:48
在编程的世界里,字符串扮演着至关重要的角色,它是信息传递和数据处理的基础。而Java中String类的字符串方法,更是为开发者提供了丰富的操作和处理字符串的工具,让我们一起来开启一场探索之旅,揭秘这些方法的魅力。
Java中的String字符串方法
1. length()
这个方法返回字符串的长度,也就是字符的数量。在实际应用中,length()方法可以帮助我们确定字符串是否为空、循环遍历字符串以及截取字符串的子串。
String str = "Hello World!";
int length = str.length(); // 12
2. charAt()
charAt()方法返回指定索引处的字符。索引是从0开始的,如果索引超出字符串的长度,就会抛出StringIndexOutOfBoundsException异常。
String str = "Hello World!";
char ch = str.charAt(0); // 'H'
3. codePointAt()
codePointAt()方法返回指定索引处的代码点。代码点是Unicode字符的唯一标识符,它可以是单个字符,也可以是多个字符组合而成。
String str = "Hello \u00a9 World!";
int codePoint = str.codePointAt(0); // 72
4. codePointCount()
codePointCount()方法返回字符串中代码点的数量。
String str = "Hello \u00a9 World!";
int codePointCount = str.codePointCount(0, str.length()); // 13
5. compareTo()
compareTo()方法比较两个字符串,返回一个整数。如果第一个字符串大于第二个字符串,则返回正整数;如果第一个字符串小于第二个字符串,则返回负整数;如果两个字符串相等,则返回0。
String str1 = "Hello";
String str2 = "World";
int result = str1.compareTo(str2); // -5
6. compareToIgnoreCase()
compareToIgnoreCase()方法比较两个字符串,忽略大小写。如果第一个字符串大于第二个字符串,则返回正整数;如果第一个字符串小于第二个字符串,则返回负整数;如果两个字符串相等,则返回0。
String str1 = "Hello";
String str2 = "world";
int result = str1.compareToIgnoreCase(str2); // 0
7. concat()
concat()方法将两个字符串连接成一个新的字符串。
String str1 = "Hello";
String str2 = "World!";
String str3 = str1.concat(str2); // "Hello World!"
8. contains()
contains()方法检查字符串是否包含指定的子字符串。如果包含,则返回true;否则,返回false。
String str = "Hello World!";
boolean result = str.contains("World"); // true
9. startsWith()
startsWith()方法检查字符串是否以指定的子字符串开头。如果以指定的子字符串开头,则返回true;否则,返回false。
String str = "Hello World!";
boolean result = str.startsWith("Hello"); // true
10. endsWith()
endsWith()方法检查字符串是否以指定的子字符串结尾。如果以指定的子字符串结尾,则返回true;否则,返回false。
String str = "Hello World!";
boolean result = str.endsWith("World!"); // true
11. indexOf()
indexOf()方法返回指定子字符串在字符串中第一次出现的位置。如果子字符串没有出现在字符串中,则返回-1。
String str = "Hello World!";
int index = str.indexOf("World"); // 6
12. lastIndexOf()
lastIndexOf()方法返回指定子字符串在字符串中最后一次出现的位置。如果子字符串没有出现在字符串中,则返回-1。
String str = "Hello World!";
int index = str.lastIndexOf("World"); // 6
13. replace()
replace()方法将字符串中的指定子字符串替换为另一个子字符串。
String str = "Hello World!";
String newStr = str.replace("World", "Universe"); // "Hello Universe!"
14. replaceAll()
replaceAll()方法将字符串中的所有匹配子字符串替换为另一个子字符串。
String str = "Hello World!";
String newStr = str.replaceAll("\\s", ""); // "HelloWorld!"
15. replaceFirst()
replaceFirst()方法将字符串中的第一个匹配子字符串替换为另一个子字符串。
String str = "Hello World!";
String newStr = str.replaceFirst("World", "Universe"); // "Hello Universe!"
16. split()
split()方法将字符串按照指定的分隔符拆分成一个字符串数组。
String str = "Hello,World,Universe";
String[] arr = str.split(","); // ["Hello", "World", "Universe"]
17. toCharArray()
toCharArray()方法将字符串转换为一个字符数组。
String str = "Hello World!";
char[] arr = str.toCharArray(); // ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!']
18. toString()
toString()方法将字符串转换为一个字符串。
String str = "Hello World!";
String newStr = str.toString(); // "Hello World!"
19. trim()
trim()方法删除字符串两端的空格。
String str = " Hello World! ";
String newStr = str.trim(); // "Hello World!"