String中的常用API(三)

学习整理之String中常用的API(三)

(24)byte[] getBytes():编码,把字符串变为字节数组,按照平台默认的字符编码进行编码

    @Test
    public void test24() {
        String s1 = new String("a");
        //按照平台默认的字符编码进行编码
        byte[] bytes1 = s1.getBytes();
        for (int i = 0; i < bytes1.length; i++) {
            System.out.println("[" + bytes1[i] + "]");//[97]
        }

        String s2 = new String("吉");
        //按照平台默认的字符编码进行编码
        byte[] bytes2 = s2.getBytes();
        for (int i = 0; i < bytes2.length; i++) {
            System.out.print("[" + bytes2[i] + "]");//[-27][-112][-119]
        }
    }

 -  byte[] getBytes(字符编码方式):按照指定的编码方式进行编码        

 @Test
    public void test24_1() throws UnsupportedEncodingException {
        String s1 = new String("a");
        //按指定的字符编码进行编码
        byte[] bytes1 = s1.getBytes("GBK");
        for (int i = 0; i < bytes1.length; i++) {
            System.out.println("[" + bytes1[i] + "]");//[97]
        }

        String s2 = new String("吉");
        //按指定的字符编码进行编码
        byte[] bytes2 = s2.getBytes("GBK");
        for (int i = 0; i < bytes2.length; i++) {
            System.out.print("[" + bytes2[i] + "]");//[-68][-86]
        }
    }

(25)new String(byte[] ) 或 new String(byte[], int, int):解码,按照平台默认的字符编码进行解码

    @Test
    public void test25() {
        byte[] bytes1 ={97};
        //按照平台默认的字符编码进行编码
        String s1 = new String(bytes1);
        System.out.println(s1);//a

        byte[] bytes2 ={-27,-112,-119};
        //按照平台默认的字符编码进行编码
        String s2 = new String(bytes2);
        System.out.println(s2);//吉
    }


​ -  new String(byte[],字符编码方式 ) 或 new String(byte[], int, int,字符编码方式):解码,按照指定的编码方式进行解码

    @Test
    public void test25_1() throws UnsupportedEncodingException {
        byte[] bytes1 ={97};
        //按指定的字符编码进行编码
        String s1 = new String(bytes1,"GBK");
        System.out.println(s1);//a

        byte[] bytes2 ={-68,-86};
        //按指定的字符编码进行编码
        String s2 = new String(bytes2,"GBK");
        System.out.println(s2);//吉
    }

(26)boolean startsWith(xx):是否以xx开头

    @Test
    public void test26(){
        String s = new String("helloworld");
        //判断字符串s是否以hello开头
        boolean b1 = s.startsWith("hello");
        System.out.println(b1);//true

        //判断字符串s是否以world开头
        boolean b2 = s.startsWith("world");
        System.out.println(b2);//false
    }

(27)boolean endsWith(xx):是否以xx结尾

    @Test
    public void test27(){
        String s = new String("helloworld");
        //判断字符串s是否以hello结尾
        boolean b1 = s.endsWith("hello");
        System.out.println(b1);//false

        //判断字符串s是否以world开头
        boolean b2 = s.endsWith("world");
        System.out.println(b2);//true
    }

(28)boolean matches(正则表达式):判断当前字符串是否匹配某个正则表达式

   @Test
    public void test28() {
        String s1 = "12345678";
        // \在Java中有特殊意义,转义
        //判断s1是否是八位数字
        System.out.println(s1.matches("\\d{8}"));//true

        String s2 = "123456789";
        // \在Java中有特殊意义,转义
        //判断s2是否是八位数字
        System.out.println(s2.matches("\\d{8}"));//false
    }

(29)String replace(xx,xx):替换字符串,不支持正则

    @Test
    public void test29(){
        String s = new String("今天0是国庆0假期的第0四天,天气0阴,又是天0气不好的0一天");
        //把s中的0全部替换成"";
        String replace1 = s.replace("0", "");
        System.out.println(replace1);//今天是国庆假期的第四天,天气阴,又是天气不好的一天

        //因为replace不支持正则表达式,所以不能用正则进行替换
        String replace2 = s.replace("\\d+", "");
        System.out.println(replace2);//今天0是国庆0假期的第0四天,天气0阴,又是天0气不好的0一天
    }

(30)String replaceFirst(正则,value):替换第一个匹配部分

    @Test
    public void test30(){
        String s = new String("今天0是国庆0假期的第0四天,天气0阴,又是天0气不好的0一天");
        //去掉s字符串中的数字,第一个0
        String replace = s.replaceFirst("\\d+","");
        //因为replaceFirst只能替换字符串中第一个匹配的字符,所以[2]位置上的0被替换成了"";
        System.out.println(replace);//今天是国庆0假期的第0四天,天气0阴,又是天0气不好的0一天
    }

(31)String repalceAll(正则, value):替换所有匹配部分

    @Test
    public void test31(){
        String s = new String("今天0是国庆0假期的第0四天,天气0阴,又是天0气不好的0一天");
        //去掉s字符串中的所有数字
        String replace = s.replaceAll("\\d+","");
        //因为replaceAll是替换字符串中所有匹配的字符,所以s中的所有数字全被替换成了"";
        System.out.println(replace);//今天是国庆假期的第四天,天气阴,又是天气不好的一天
    }

(32)String[] split(正则):按照某种规则进行拆分

    @Test
    public void test32(){
        String s = "0Head23First32Java45";
        //去除首位的数字
        s = s.replaceAll("^\\d+|\\d+$","");
        System.out.println("s = " + s);//s = Head23First32Java
        //根据数字对字符串拆分
        String[] strings = s.split("\\d+");
        System.out.println("字符串个数:" + strings.length);//字符串个数:3
        for (int i = 0; i < strings.length; i++) {
            System.out.print("[" + strings[i] + "]");//[Head][First][Java]
        }
    }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zwjStart

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

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

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

打赏作者

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

抵扣说明:

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

余额充值