Java String 常用操作方法说明和使用

ps:Java中的String类是一个非常重要的类,在Java程序中广泛使用。它可以用来保存和操作字符串,在这篇博客中,我们将对Java String的所有操作方法进行说明和使用。

1.1 使用双引号创建字符串

String str1 = "Hello, World!";

1.2 使用String构造方法创建字符串

String str2 = new String("Hello, World!");

1.3 使用字符数组创建字符串

char[] charArray = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
String str3 = new String(charArray);

2、获取长度

使用length()方法获取字符串的长度。

String str = "Hello, World!";
int length = str.length(); // 13

3、获取指定位置字符

使用charAt()方法获取指定位置的字符,索引从0开始。

String str = "Hello, World!";
char ch = str.charAt(4); // 'o'

4、拼接字符串

4.1 使用加号拼接字符串

String str1 = "Hello, ";
String str2 = "World!";
String result = str1 + str2; // "Hello, World!"

4.2 使用StringBuilder拼接字符串

StringBuilder sb = new StringBuilder();
sb.append("Hello, ");
sb.append("World!");
String result = sb.toString(); // "Hello, World!"

5、替换字符串

使用replace()方法替换字符串中的字符。

String str = "Hello, World!";
String result = str.replace("l", "x"); // "Hexxo, Worxd!"

6、截取子串

6.1 使用substring()方法截取部分字符

String str = "Hello, World!";
String result = str.substring(4, 9); // "o, Wo"

6.2 使用split()方法拆分字符串并截取部分字符

String str = "Hello, World!";
String[] strArray = str.split(",");
String result = strArray[0].substring(4); // "o"

7、转换大小写

7.1 将字符串转换为大写

String str = "Hello, World!";
String result = str.toUpperCase(); // "HELLO, WORLD!"

7.2 将字符串转换为小写

String str = "Hello, World!";
String result = str.toLowerCase(); // "hello, world!"

8、去掉空格

8.1 去掉字符串两端的空格

String str = "   Hello, World!   ";
String result = str.trim(); // "Hello, World!"

8.2 去掉字符串中的所有空格

String str = "  H e l lo ,  W orl d !  ";
String result = str.replace(" ", ""); // "Hello,World!"

9、拆分字符串

使用split()方法将字符串拆分为多个子串。

String str = "apple,banana,orange";
String[] strArray = str.split(",");

10、包含子串

使用contains()方法判断字符串是否包含指定的子串。

String str = "Hello, World!";
boolean contains = str.contains("World"); // true

11、判断字符串是否为空

使用isEmpty()方法判断字符串是否为空。

String str1 = "";
String str2 = null;
boolean isEmpty1 = str1.isEmpty(); // true
boolean isEmpty2 = str2.isEmpty(); // NullPointerException

12、比较字符串

12.1 使用equals()方法比较字符串是否相等

String str1 = "Hello, World!";
String str2 = "Hello, World!";
boolean isEqual1 = str1.equals(str2); // true

12.2 使用equalsIgnoreCase()方法比较字符串是否相等(忽略大小写)

String str1 = "Hello, World!";
String str2 = "HELLO, WORLD!";
boolean isEqual2 = str1.equalsIgnoreCase(str2); // true

13、查找某字符串位置

使用indexOf()方法查找指定子串在字符串中的位置,未找到返回-1。

String str = "Hello, World!";
int index = str.indexOf("World"); // 7

14、转换为字符数组

使用toCharArray()方法将字符串转换为字符数组。

String str = "Hello, World!";
char[] charArray = str.toCharArray();

15、格式化字符串

使用String.format()方法格式化字符串,并输出到控制台。

String str = "Hello, World!";
System.out.printf("Output: [%20s]\n", str);
// Output: [Hello, World!]

总结

本篇博客介绍了Java String的常用操作方法,包括创建字符串、获取字符串长度、获取指定位置的字符、字符串连接、比较字符串、获取子字符串、转换大小写、替换字符、分割字符串、转换为字符数组、判断是否包含某个字符串、判断是否以某个字符串开头或结尾、格式化字符串等。这些方法广泛应用于Java程序开发中,掌握它们将有助于编写更高效的Java代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肖肖依旧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值