java string类的常用方法
时间: 2023-06-16 17:08:27 浏览: 144
Java中的String类是一个非常常用的类,提供了丰富的方法来操作字符串。下面是一些常用的String类方法:
1. length():返回字符串长度。
2. charAt(int index):返回指定位置的字符。
3. substring(int beginIndex):返回从指定位置到字符串末尾的子字符串。
4. substring(int beginIndex, int endIndex):返回从指定位置到指定位置的子字符串。
5. equals(Object anObject):比较两个字符串是否相等,区分大小写。
6. equalsIgnoreCase(String anotherString):比较两个字符串是否相等,不区分大小写。
7. compareTo(String anotherString):按字典顺序比较两个字符串,返回比较结果。
8. compareToIgnoreCase(String str):按字典顺序比较两个字符串,不区分大小写,返回比较结果。
9. indexOf(int ch):返回指定字符在字符串中第一次出现的位置。
10. indexOf(String str):返回指定字符串在字符串中第一次出现的位置。
11. lastIndexOf(int ch):返回指定字符在字符串中最后一次出现的位置。
12. lastIndexOf(String str):返回指定字符串在字符串中最后一次出现的位置。
13. toLowerCase():将字符串转换为小写。
14. toUpperCase():将字符串转换为大写。
15. trim():去除字符串两端的空白符。
16. replace(char oldChar, char newChar):替换字符串中的字符。
17. replaceAll(String regex, String replacement):替换字符串中的匹配子串。
18. split(String regex):按照指定的正则表达式分割字符串。
19. startsWith(String prefix):判断字符串是否以指定前缀开头。
20. endsWith(String suffix):判断字符串是否以指定后缀结尾。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)