Java学习历程十三《工具类之字符串》

Java中的字符串

  • String类
  • StringBuilder类

String对象的方法

创建对象:

方式一:String s1 = "hello world"

方式二:String s2 = new String()

方式三:String s2 = new String("hello world")

常用方法:

package TestString;

import java.io.UnsupportedEncodingException;

public class StringDemoOne {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s1 = "hello";
        int res1 = s1.length(); // 返回字符串的长度
        System.out.println(res1);
        int res2 = s1.indexOf('e'); // 返回某个字符在字符串中首次出现的位置
        System.out.println(res2);
        int res3 = s1.indexOf("llo");
        System.out.println(res3); // 返回某个字串在字符串中首次出现的位置
        int res4 = s1.lastIndexOf('o'); // 返回某个字符在最后一次出现的位置
        System.out.println(res4);
        int res5 = s1.lastIndexOf("el"); // 返回某个字串在最后一次出现的位置
        System.out.println(res5);
        String res6 = s1.substring(3); // 给定开始位置,返回从给定位置到最后的字串
        System.out.println(res6);
        String res7 = s1.substring(3, 4); // 给定开始和结束位置,返回给定范围的字串[begin,end)左闭右开
        System.out.println(res7);
        String res8 = s1.trim(); // 返回去除首尾空格的字符串
        System.out.println(res8);
        boolean res9 = s1.equals("hello"); // 比较两个字符串
        String res10 = s1.toLowerCase(); // 全部转换为小写
        String res11 = s1.toUpperCase();// 全部转换为大写
        char res12 = s1.charAt(4); // 获取字符串中指定位置的字符
        String[] res13 = s1.split("l"); // 将字符串按照指定规则切分,可以规定切分次数,注意:被切部分不保留
        for (String i : res13) {
            System.out.print(i+" ");
        }
        System.out.println();
        byte[] res14 = s1.getBytes();// 将字符串转换byte数组
        for (byte i : res14) {
            System.out.print(i+" ");
        }
        System.out.println();
        // 将byte数组转换为String
        String res15 = new String(res14);
        System.out.println(res15);

    }
}

比较==符号和equals()方法的区别:

首先equals()方法对字符串对象的比较,比较的是内容,只要内容是相同的,返回的结果就是true
==比较的是两个对象的内存地址,

package TestString;

public class StringDemoTwo {
    public static void main(String[] args) {
        String st1 = "this is st";
        String st2 = "this is st";
        String st3 = new String("this is st");
        System.out.println("st1和st2的内容是否相同:" + st1.equals(st2));
        System.out.println("st1和st3的内容是否相同:" + st1.equals(st3));
        System.out.println("st1和st2的地址是否相同:" + (st1==st2));
        System.out.println("st1和st3的地址是否相同:" + (st1==st3));
    }
}

结果为:

st1和st2的内容是否相同:true
st1和st3的内容是否相同:true
st1和st2的地址是否相同:true
st1和st3的地址是否相同:false

字符串属于常量,是被储存在常量池中,当执行String st1 = "hello",那么就会在常量池中形成"hello",在栈中形成st1,然后st1指向"hello",
当继续执行String st2 = "hello"的时候,就会在栈中形成st2直接指向常量池中已存在的"hello",然而执行String st3 = new String("hello")
的时候,会在栈中形成st3,然后再在堆中new String("hello")对象,这样通过==比较就会返回false

String 的不可变性

String对象一旦被创建,数据不可变

package TestString;

public class StringDemoThree {
    public static void main(String[] args) {
        String st1 = "hello";
        st1 += "-hello";
        System.out.println(st1);
    }
}

结果为:

hello-hello

当指向上述代码的时候:

1. 栈st1 -> 常量池"hello"
2. 栈中的st1 -> 常量池中新生成的"hello-hello"
package TestString;

public class StringDemoThree {
    public static void main(String[] args) {
        String st1 = "hello";
        String st2 = st1+"-hello";
        System.out.println(st1);
        System.out.println(st2);
    }
}

结果为:

hello
hello-hello

会发现st1没有变,栈中新生成st2指向,常量池中新生成"hello-hello"

字符串类具有不可变性,那么在程序运行过程中,如果字符串常常在变化,那么会形
很多中间变量,这一点是不可变性带来的局限性.

StringBuilder

主要区别在:
String具有不可变性,而StringBuilder不具备不可变性

所以当频繁的操作某字符串的时候,选择StringBuilder,来避免,处理过程中产生的中间变量

StringBuilder和StringBurrer的比较:
二者基本相似,在程序开发的时候可以互相替代,但是由于StringBuffer是具有线程安全的,然而
StringBuilder是不具备线程安全的,所以StringBuilder的效率高一些

常用方法

public class StringBuilderDemo1 {
    public static void main(String[] args) {
        //定义字符串的对象
        StringBuilder stb = new StringBuilder("你好");
        //在你好的后面添加内容 你好,java
        stb.append(',');
        stb.append("Java!");
        System.out.println("str=" + stb);
        //两种方式实现小写->大写的转换
        //1.delete,删除小写的,再插入大写的
        //System.out.println("替换后="+stb.delete(3,7).insert(3,"JAVA"));
        //2.使用replace方法直接替换
        //System.out.println("replace替换后:"+stb.replace(3,7,"JAVA"));
        //去除子串
        //System.out.println(stb.substring(0,2));
    }
}

当我么创建StringBuilder对象的时候:

栈中’std’ --> 堆中’new StringBuilder(“你好”)’

当执行append的时候,会继续在堆中的对象的数据的后面添加数据(“你好,java”)

对比String,String生成新的对象,而不是修改数据

所以StringBuilder对象可以进行链式的操作

串尾添加 append
删除 delete
插入insert
替换replace

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值