Java的string、StringBuffer、StringBuilder

目录

2.String

2.1字符串本质

2.2构造方法

2.2字符串的相等比较

2.3查找、判断字符串

2.4大小写转换

2.5字符串长度、去空格、分割

2.6字符串截取、替换、连接

2.7 字符串转变为Byte数组、字符数组

3.StringBuffer

4.StringBuilder

1.三者的区别

String、StringBuffer和StringBuilder的区别 -   https://blog.csdn.net/csxypr/article/details/92378336

StringBuilder类也代表可变字符串对象。实际上,StringBuilder和StringBuffer基本相似,两个类的构造器和方法也基本相同。不同的是:StringBuffer是线程安全的,而StringBuilder则没有实现线程安全功能,所以性能略高。

2.String

JAVA String字符串的常用方法 - 知乎  https://zhuanlan.zhihu.com/p/64093275

java中常用的String方法 - 阿溪 - 博客园  https://www.cnblogs.com/liujiquan/p/7808501.html

2.1字符串本质

String 类代表字符串。Java 程序中的所有字符串字面值(如“abc”)都作为此类的对象。字符串本质上是一个字符数组,它们的值在创建后不能被更改,所以字符串是常量;可以把字符串看成是字符数组的包装类,内部声明一个 private final char value [ ];因为String 对象是不可变的,所以可以共享。

2.2构造方法

    public static void main(String[] args) {
        // 在堆区初始化一个空字符串
        String str1 = new String();

        // 通过一个字节数组构建一个字符串
        byte[] bytes = {97,98,99};
        // 通过使用平台的默认字符集解码指定的 byte 数组
        // System.out.println(Charset.defaultCharset());
        String str2 = new String(bytes);
        System.out.println(str2);//abc

        byte[] byte2 = {-42,-48};
        String str3 = null;
        try {
            // 使用指定的字符集对字节序列进行解码
            str3 = new String(byte2,"GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(str3);//中

        // 需求:对一个utf-8的字节序列进行解码
        byte[] byte3 = {-28,-72,-83,-28,-72,-83};
        try {
            // sssString str4 = new String(byte3, "UTF-8");
            String str4 = new String(byte3,0,3, "UTF-8");
            System.out.println("str4:"+str4);//str4:中
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 通过字符数组构建字符串
        char[] c1 = {'a','b','c','中','国'};
        // String str5 = new String(c1);
        String str5 = new String(c1,0,3);
        System.out.println(str5);//abc

        String str6 = new String("abc");
        String str7 = "abc";
        System.out.println(str6 == str7);//false

    }

2.2字符串的相等比较

        String a = "Hello Word";
        String b = "hello word";
        System.out.println(a == b);//false
        System.out.println(a.equals(b));//false
        System.out.println(a.equalsIgnoreCase(b));//true
        String c = a;
        System.out.println(a == c);//true
        System.out.println(a.equals(c));//true
        System.out.println(a.equalsIgnoreCase(c));//true

        //compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小
        String a = "Hello Word";
        String b = "hello word";
        System.out.println(a.compareTo(b));//-32
        System.out.println(a.compareToIgnoreCase(b));//0

2.3查找、判断字符串

        //获取字符串指定位置的字符
        String str="我是123,123。123";
        char indexChar = str.charAt(0);
        System.out.println(indexChar);//我
        //获取字符串指定范围的字符数组
        String a = "Hello Word";
        char[] b = new char[10];
        a.getChars(0, 5, b, 0);
        System.out.println(b);//Hello
        //查找某个字符在字符串中首次出现的位置
        int firstIndex = str.indexOf("1");
        System.out.println(firstIndex);//2
        //查找某个字符在字符串中最后出现的位置
        int lastIndex = str.lastIndexOf("1");
        System.out.println(lastIndex);//10
        boolean contains = str.contains("10");
        System.out.println(contains);//false
        //字符串开头和结尾的判断
        String a = "Hello Word";
        System.out.println(a.startsWith("ee"));//false
        System.out.println(a.startsWith("He"));//true
        System.out.println(a.endsWith("rd"));//true
        System.out.println(a.endsWith("r"));//false
        String str="我是123,123。123";
        boolean startWith=str.startsWith("我");
        boolean endWith=str.endsWith("3");
        //是否为空
        String a = "";
        System.out.println(a.isEmpty());//true
        String b = " ";
        System.out.println(b.isEmpty());//false

2.4大小写转换

        String str="我是Aa";
        //将字母全部转换成大写
        String lowerCase = str.toLowerCase();
        System.out.println(lowerCase);//我是aa
        //将字母全部转换成小写
        String upperCase = str.toUpperCase();
        System.out.println(upperCase);//我是AA

2.5字符串长度、去空格、分割

        String str=" 我是123,123。123 ";
        //获取字符串长度
        int strLenth = str.length();//15
        System.out.println(strLenth);
        //去除字符串中的首尾空格
        String newStr = str.trim();//我是123,123。123
        System.out.println(newStr);
        //将字符串分割,多个分隔符可用|隔开,例如下面这个是按照“。”和“,”分割的
        String[] splite = str.trim().split("。|,");//
        for (int i = 0; i < splite.length; i++)
            System.out.println(splite[i]);
        //我是123
        //123
        //123

2.6字符串截取、替换、连接

        //截取字符串:
        String str="我是123,123。123"; 
        String subStr = str.substring(0,2);//我是
        //截取字符串:输入为字符数组
        char[] Str1 = {'h', 'e', 'l', 'l', 'o', ' ', 's', 'u', 'n', 'n', 'y', '!'};
        String Str2 = "";
        Str2 = Str2.copyValueOf( Str1 );
        System.out.println("返回结果:" + Str2);//返回结果:hello sunny!
        Str2 = Str2.copyValueOf( Str1, 2, 6 );
        System.out.println("返回结果:" + Str2);//返回结果:llo su
        //字符串替换
        String str="我是123,123。123";
        String reStr = str.replace("1", "4");//我是423,423。423
        String firReStr = str.replaceFirst("1", "4");//我是423,123。123
        //正则表达式替换
        String Str = new String("www.google.com");
        System.out.println(Str.replaceAll("(.*)google(.*)", "runoob" ));//runoob
        System.out.println(Str.replaceAll("(.*)taobao(.*)", "runoob" ));//www.google.com
        //连接字符串:concact
        String a = "Hello Word";
        String b = "你好";
        System.out.println(b.concat(a));//你好Hello Word
        System.out.println(b+a);//你好Hello Word
        //连接字符串:join
        List names=new ArrayList<String>();
        names.add("1");
        names.add("2");
        names.add("3");
        System.out.println(String.join("-", names));//1-2-3
        String[] arrStr=new String[]{"a","b","c"};
        System.out.println(String.join("-", arrStr));//a-b-c

2.7 字符串转变为Byte数组、字符数组

        String a = "Hello Word";
        //getBytes()将字符串变成一个byte数组
        byte[] b = a.getBytes();
        System.out.println(b);//[B@1cd072a9
        System.out.println(new String(b));//Hello Word
        //toCharArray()将字符串变成一个字符数组
        char[] c = a.toCharArray();
        System.out.println(c);//Hello Word

2.8字符串与基本类型的转换

        //String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 ,
        //也就是 String.valueOf() 这个参数多载的方法         
        int i = 10;
        String str = String.valueOf(i);
        System.out.println(str);//10

        //要将 String 转换成基本数据型态转 ,大多需要使用基本数据型态的包装类别 
        Integer i = 10;
        String str = String.valueOf(i);
        System.out.println(str instanceof String);//true
        Short j = Short.parseShort(str);
        System.out.println(j instanceof Short);//true

3.StringBuffer

public class 字符串 {
    public static void main(String args[]){
        StringBuffer strBuffer1 = new StringBuffer("Hello");
        StringBuffer strBuffer2 = new StringBuffer("*");
        //末尾添加
        strBuffer1.append(strBuffer2);
        strBuffer1.append("wo"+"rld");
        strBuffer1.append("!");
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Hello*world!
        // 将指定的字符串插入字符串序列
        strBuffer1.insert(0, "Ni Hao, ");
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao, Hello*world!

        // 删除指定位置字符串
        strBuffer1.delete(strBuffer1.indexOf("N"),strBuffer1.indexOf("*"));
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = *world!
        strBuffer1.deleteCharAt(strBuffer1.indexOf("*"));
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = world!

        //替换指定范围字符串
        strBuffer1.insert(0, "*");
        strBuffer1.replace(0, strBuffer1.indexOf("*"), "Ni Hao,");
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao,*world!
        // 字符串反转
        strBuffer1.reverse();
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = !dlrow ,oaH iN
        strBuffer1.reverse();
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao,*world!
        // 修改指定位置的字符
        strBuffer1.setCharAt(0, 'n');
        System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = ni Hao,*world!

        //获取StringBuffer对象的容量,和字符串的长度
        System.out.println("strBuffer1.capacity() = "+strBuffer1.capacity());//strBuffer1.capacity() = 21
        System.out.println("strBuffer1.length() = "+strBuffer1.length());//strBuffer1.length() = 14
        //查找指定字符位置
        System.out.println(strBuffer1.indexOf("o"));//5
        System.out.println(strBuffer1.indexOf("o",6));//9
        System.out.println(strBuffer1.lastIndexOf("o"));//9
        System.out.println(strBuffer1.lastIndexOf("o",6));//5
        // 比较StringBuffer字符串是否相等
        StringBuffer strBuffer3 = new StringBuffer("ni Hao,*world!");
        boolean flag = strBuffer1.toString().equals(strBuffer3.toString());
        System.out.println("strBuffer1 == strBuffer3 : "+flag);//strBuffer1 == strBuffer3 : true
        
        // 遍历StringBuffer,替换’o‘ -> 'O'
        for (int i = 0 ; i < strBuffer3.length() ; i++) {
            if (strBuffer3.charAt(i) == 'o') {
                strBuffer3.replace(i, i+1, "O");
            }
        }
        System.out.println("strBuffer3 = "+strBuffer3);//strBuffer3 = ni HaO,*wOrld!

        //获得子串
        String string1 = strBuffer1.substring(strBuffer1.indexOf("n"),strBuffer1.indexOf(","));
        System.out.println("string1 = "+string1);//strBuffer4 = ni Hao
    }
}

4.StringBuilder

StringBuffer是线程安全的,StringBuilder不是线程安全的,方法基本一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值