16-Java-字符串


字符串

直接创建

/**
 * @Author: 大海
 * @Date: 2025-09-07
 */

public class study_17_字符串 {
    public static void main(String[] args) {
        // 字符串不能改变

        // 1. 直接赋值创建
        String str1 = "hello world";
        System.out.println(str1);
  
    }
}

new方式创建
/**
 * @Author: 大海
 * @Date: 2025-09-07
 */

public class study_17_字符串 {
    public static void main(String[] args) {
        // 字符串不能改变     

        // 2. new 方式创建
        String str2 = new String("hello world");
        System.out.println(str2);

        // 字符数组转字符串
        char[] chars = {'h', 'e', 'l', 'l', 'o'};
        String str3 = new String(chars);
        System.out.println(str3);

        // 字节数组转字符串
        byte[] bytes = {97, 98, 99, 100};
        String str4 = new String(bytes);
        System.out.println(str4);

     
    }
}

方法的使用

/**
 * @Author: 大海
 * @Date: 2025-09-07
 */

public class study_17_字符串 {
    public static void main(String[] args) {
        // 字符串不能改变

        // 字符串方法使用
        // 字符串比较
        String str5 = "hello";
        String str6 = new String("hello");
        // 字符串比较时,使用 == 运算符比较的是引用地址,而不是内容是否相同
        System.out.println(str5 == str6); // false
        // 使用 equals 方法比较内容是否相同
        System.out.println(str5.equals(str6)); // true

        // 字符串拼接
        String str7 = "hello" + "world";
        System.out.println(str7);

        // 字符串长度
        String str8 = "hello world";
        System.out.println(str8.length());

        // 字符串截取
        String str9 = "hello world";
        System.out.println(str9.substring(0, 5)); // hello
        System.out.println(str9.substring(6)); // world

        // 字符串查找
        String str10 = "hello world";
        System.out.println(str10.indexOf("l")); // 2
        System.out.println(str10.lastIndexOf("l")); // 9

        // 字符串替换
        String str11 = "hello world";
        System.out.println(str11.replace("l", "L")); // heLLo world

        // 字符串比较
        String str12 = "hello";
        String str13 = "world";
        // 字符串比较时,使用 compareTo 方法比较字符串内容
        System.out.println(str12.compareTo(str13)); // -1
        System.out.println(str13.compareTo(str12)); // 1
        System.out.println(str12.compareTo(str12)); // 0
    }
}

StringBuilder

  • 可以看成是一个容器,创建之后里面的内容是可变的
  • 作用:提高字符串的操作效率
  • 应用场景: 字符串拼接和反转
/**
 * @Author: 大海
 * @Date: 2025-09-07
 */

public class study_17_字符串 {
    public static void main(String[] args) {
      
        // StringBuilder 类
        // 字符串拼接时,使用 StringBuilder 类比直接使用 + 运算符更高效
        StringBuilder str14 = new StringBuilder();
        // 链式调用
        str14.append("hello").append(" ").append("world");
        System.out.println(str14.toString());

        // 字符串替换时,使用 StringBuilder 类比直接使用 replace 方法更高效
        StringBuilder str15 = new StringBuilder("hello world");
        str15.replace(6, 11, "java");
        System.out.println(str15.toString());

        // 字符串反转
        String str16 = "hello world";
        StringBuilder str17 = new StringBuilder(str16);
        str17.reverse();
        System.out.println(str17.toString());
    }
}

StringJoiner
  • JDK8出现的一个可变的操作字符串的容器,可以高效,方便的拼接字符串。
import java.util.StringJoiner;

/**
 * @Author: 大海
 * @Date: 2025-09-07
 */

public class study_17_字符串 {
    public static void main(String[] args) {
      
        // StringJoiner 类  jdk1.8 引入
        // 字符串拼接时,使用 StringJoiner 类比直接使用 + 运算符更高效
        StringJoiner str18 = new StringJoiner("-", "(", ")");   // 间隔符、前缀、后缀
        str18.add("a").add("b").add("c");
        System.out.println(str18.toString());
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱学习de测试小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值