欢迎访问宙启技术站
智能推送

Java字符串处理函数库大全:常见字符串操作函数及用法详解

发布时间:2023-06-26 06:57:48

Java作为一门经典的编程语言,其字符串处理函数库也十分强大。在Java中,字符串被视为对象,因此有着丰富的方法来操作它们。接下来,我们将介绍一些常用的字符串处理函数及其用法,希望能帮助读者更好地理解和掌握Java中的字符串操作技能。

1. length()方法

length()方法用于返回字符串中的字符数。例如:

String str = "Hello world!";
int len = str.length();
System.out.println("字符串长度为:" + len);

输出:

字符串长度为:12

2. charAt()方法

charAt()方法用于返回指定位置的字符。例如:

String str = "Hello world!";
char ch = str.charAt(4);
System.out.println("第5个字符是:" + ch);

输出:

第5个字符是:o

3. substring()方法

substring()方法用于返回指定索引范围内的子字符串。例如:

String str = "Hello world!";
String substr = str.substring(1, 5);
System.out.println("子字符串是:" + substr);

输出:

子字符串是:ello

4. indexOf()方法

indexOf()方法用于返回指定字符串在主字符串中的位置。例如:

String str = "Hello world!";
int index = str.indexOf("world");
System.out.println("world在字符串\"" + str + "\"中的位置:" + index);

输出:

world在字符串"Hello world!"中的位置:6

5. lastIndexOf()方法

lastIndexOf()方法与indexOf()方法类似,只不过返回最后一次出现的位置。例如:

String str = "Hello world!";
int index = str.lastIndexOf("o");
System.out.println("o在字符串\"" + str + "\"中最后出现的位置:" + index);

输出:

o在字符串"Hello world!"中最后出现的位置:7

6. replace()方法

replace()方法用于在字符串中替换字符或者字符串。例如:

String str = "Hello world!";
String newStr = str.replace("o", "*");
System.out.println("替换后的字符串为:" + newStr);

输出:

替换后的字符串为:Hell* w*rld!

7. toLowerCase()方法

toLowerCase()方法用于将字符串全部转换为小写。例如:

String str = "Hello world!";
String newStr = str.toLowerCase();
System.out.println("转换后的字符串为:" + newStr);

输出:

转换后的字符串为:hello world!

8. toUpperCase()方法

toUpperCase()方法用于将字符串全部转换为大写。例如:

String str = "Hello world!";
String newStr = str.toUpperCase();
System.out.println("转换后的字符串为:" + newStr);

输出:

转换后的字符串为:HELLO WORLD!

9. trim()方法

trim()方法用于去除字符串两端的空格。例如:

String str = "  Hello world!  ";
String newStr = str.trim();
System.out.println("去除空格后的字符串为:" + newStr);

输出:

去除空格后的字符串为:Hello world!

10. valueOf()方法

valueOf()方法用于将其他类型转换为字符串。例如:

int num = 123;
String str = String.valueOf(num);
System.out.println("转换后的字符串为:" + str);

输出:

转换后的字符串为:123

11. split()方法

split()方法用于将字符串分割为数组。例如:

String str = "1,2,3,4,5";
String[] arr = str.split(",");
System.out.println("分割后的数组为:");
for (String s : arr) {
    System.out.print(s + " ");
}

输出:

分割后的数组为:
1 2 3 4 5

12. startsWith()方法

startsWith()方法用于判断字符串是否以指定字符串开头。例如:

String str = "Hello world!";
boolean flag = str.startsWith("Hello");
System.out.println("字符串是否以Hello开头:" + flag);

输出:

字符串是否以Hello开头:true

13. endsWith()方法

endsWith()方法用于判断字符串是否以指定字符串结尾。例如:

String str = "Hello world!";
boolean flag = str.endsWith("world!");
System.out.println("字符串是否以world!结尾:" + flag);

输出:

字符串是否以world!结尾:true

14. equals()方法

equals()方法用于比较两个字符串是否相等。注意,Java中字符串的比较不能使用"==",而必须使用equals()方法。例如:

String str1 = "Hello world!";
String str2 = "hello world!";
boolean flag = str1.equals(str2);
System.out.println("str1和str2是否相等:" + flag);

输出:

str1和str2是否相等:false

15. compareTo()方法

compareTo()方法用于比较两个字符串的大小关系,如果比较的两个字符串相等,则返回0;如果第一个字符串大于第二个字符串,则返回正数;如果第一个字符串小于第二个字符串,则返回负数。例如:

String str1 = "Hello";
String str2 = "World";
int result = str1.compareTo(str2);
System.out.println("比较结果为:" + result);

输出:

比较结果为:-15

以上是一些常见的Java字符串处理函数及其用法。当然,Java中还有很多其他有用的字符串操作函数,希望读者可以多加研究和实践。