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

Java中字符串操作的常用函数

发布时间:2023-06-08 16:46:43

字符串是Java中重要的数据类型之一,它在Java语言中有着重要的地位。随着字符串处理场景的不断增多,Java中的字符串操作函数也越来越多。下面是Java中字符串操作的常用函数。

1. length()

函数的作用:返回调用字符串的长度。

示例:

String str = "hello, world!";
int len = str.length();
System.out.println("字符串长度为:" + len); // 输出:字符串长度为:13

2. charAt(int index)

函数的作用:返回一个指定索引处的字符。

示例:

String str = "hello, world!";
char ch = str.charAt(1);
System.out.println("索引为1的字符为:" + ch); // 输出:索引为1的字符为:e

3. substring(int beginIndex, int endIndex)

函数的作用:返回一个新字符串,它是此字符串的一个子字符串。

示例:

String str = "hello, world!";
String sub = str.substring(2, 5);
System.out.println("从索引为2开始到索引为5结束的子串为:" + sub); // 输出:从索引为2开始到索引为5结束的子串为:llo

4. indexOf(String str)

函数的作用:返回指定子字符串在此字符串中第一次出现处的索引。

示例:

String str = "hello, world!";
int index = str.indexOf("world");
System.out.println("world出现在字符串中的位置为:" + index); // 输出:world出现在字符串中的位置为:7

5. lastIndexOf(String str)

函数的作用:返回指定子字符串在此字符串中最后一次出现处的索引。

示例:

String str = "hello, world!";
int index = str.lastIndexOf("l");
System.out.println("最后一个l出现在字符串中的位置为:" + index); // 输出:最后一个l出现在字符串中的位置为:10

6. replace(char oldChar, char newChar)

函数的作用:返回一个新字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的。

示例:

String str = "hello, world!";
String newStr = str.replace('l', 'L');
System.out.println("将所有l替换成L后的新字符串为:" + newStr); // 输出:将所有l替换成L后的新字符串为:heLLo, worLd!

7. trim()

函数的作用:返回字符串的副本,忽略前导空白和尾部空白。

示例:

String str = "    hello, world!   ";
String newStr = str.trim();
System.out.println("去掉空白字符后的字符串为:" + newStr); // 输出:去掉空白字符后的字符串为:hello, world!

8. toUpperCase()

函数的作用:将此字符串转换为大写。

示例:

String str = "hello, world!";
String newStr = str.toUpperCase();
System.out.println("转换为大写后的新字符串为:" + newStr); // 输出:转换为大写后的新字符串为:HELLO, WORLD!

9. toLowerCase()

函数的作用:将此字符串转换为小写。

示例:

String str = "HELLO, WORLD!";
String newStr = str.toLowerCase();
System.out.println("转换为小写后的新字符串为:" + newStr); // 输出:转换为小写后的新字符串为:hello, world!

10. startsWith(String prefix)

函数的作用:测试此字符串是否以指定的前缀开始。

示例:

String str = "hello, world!";
boolean flag = str.startsWith("he");
System.out.println("字符串是否以he开头:" + flag); // 输出:字符串是否以he开头:true

11. endsWith(String suffix)

函数的作用:测试此字符串是否以指定的后缀结束。

示例:

String str = "hello, world!";
boolean flag = str.endsWith("d!");
System.out.println("字符串是否以d!结尾:" + flag); // 输出:字符串是否以d!结尾:true

12. equals(Object obj)

函数的作用:将此字符串与指定对象比较。

示例:

String str1 = "hello, world!";
String str2 = "hello, world!";
String str3 = "HELLO, WORLD!";
boolean flag1 = str1.equals(str2);
boolean flag2 = str1.equals(str3);
System.out.println("str1与str2是否相等:" + flag1); // 输出:str1与str2是否相等:true
System.out.println("str1与str3是否相等:" + flag2); // 输出:str1与str3是否相等:false

13. equalsIgnoreCase(String anotherString)

函数的作用:将此字符串与另一个字符串比较,忽略大小写。

示例:

String str1 = "hello, world!";
String str2 = "HELLO, WORLD!";
boolean flag = str1.equalsIgnoreCase(str2);
System.out.println("str1与str2是否相等(忽略大小写):" + flag); // 输出:str1与str2是否相等(忽略大小写):true

14. compareTo(String anotherString)

函数的作用:按字典顺序比较两个字符串。

示例:

String str1 = "hello, world!";
String str2 = "abc";
String str3 = "hello, java!";
int result1 = str1.compareTo(str2);
int result2 = str1.compareTo(str3);
System.out.println("str1和str2比较的结果为:" + result1); // 输出:str1和str2比较的结果为:18
System.out.println("str1和str3比较的结果为:" + result2); // 输出:str1和str3比较的结果为:2

15. split(String regex)

函数的作用:根据给定的正则表达式拆分此字符串。

示例:

String str = "hello, world!";
String[] arr = str.split(",");
System.out.println("拆分后的数组为:");
for (String s : arr) {
    System.out.println(s);
}
// 输出:
// 拆分后的数组为:
// hello
//  world!

以上是Java中字符串操作的常用函数,这些函数在字符串操作中经常使用。掌握这些函数的使用方法,有助于提高开发效率。