栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

String中的常用API(三)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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]
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292082.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号