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

String中常用的API(二)

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

String中常用的API(二)

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

(11)boolean contains(xx):是否包含xx

    @Test
    public void test11() {
        String s1 = new String("今天是2021年10月4日,国庆假期的第四天");

        boolean b1 = s1.contains("国庆假期");//true
        System.out.println(b1);

        boolean b2 = s1.contains("第五天");
        System.out.println(b2);//false
    }

(12)int indexOf(xx):从前往后找当前字符串中xx,即如果有返回第一次出现的下标,要是没有返回-1

    @Test
    public void test12() {
        String s1 = new String("今天是2021年10月4日,国庆假期的第四天");

        int i1 = s1.indexOf("国庆假期");
        System.out.println(i1);//14

        int i2 = s1.indexOf("天");
        System.out.println(i2);//1

        int i3 = s1.indexOf("第五天");
        System.out.println(i3);//-1
    }

(13)int lastIndexOf(xx):从后往前找当前字符串中xx,即如果有返回最后一次出现的下标,要是没有返回-1

    @Test
    public void test13() {
        String s1 = new String("今天是2021年10月4日,国庆假期的第四天");

        int i1 = s1.lastIndexOf("国庆假期");
        System.out.println(i1);//14

        int i2 = s1.lastIndexOf("天");
        System.out.println(i2);//21

        int i3 = s1.lastIndexOf("第五天");
        System.out.println(i3);//-1
    }

(14)String substring(int beginIndex) :返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。

  @Test
    public void test14() {
        String s1 = new String("今天是2021年10月4日,国庆假期的第四天");
        String substring = s1.substring(14);
        System.out.println(substring);//国庆假期的第四天
    }

15)String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。

    @Test
    public void test15() {
        String s1 = new String("今天是2021年10月4日,国庆假期的第四天");
        String substring = s1.substring(14,18);
        System.out.println(substring);//国庆假期
    }

(16)char charAt(index):返回[index]位置的字符

    @Test
    public void test16() {
        String s1 = new String("helloworld");
        char c = s1.charAt(0);
        System.out.println(c);//h
    }

(17)char[] toCharArray(): 将此字符串转换为一个新的字符数组返回

    @Test
    public void test17() {
        String s1 = new String("helloworld");
        char[] chars = s1.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i] + " ");//h e l l o w o r l d 
        }
    }

(18)String(char[] value):返回指定数组中表示该字符序列的 String。

    @Test
    public void test18() {
        char[] chars = {'h','e','l','l','o'};
        String s = new String(chars);
        System.out.println(s);//hello
    }

(19)String(char[] value, int offset, int count):返回指定数组中表示该字符序列的 String。

    @Test
    public void test19() {
        char[] chars = {'h','e','l','l','o'};
        String s = new String(chars,0,2);
        System.out.println(s);//he
    }

(20)static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的 String

    @Test
    public void test20() {
        char[] chars = {'h','e','l','l','o'};
        String s = String.copyValueOf(chars);
        System.out.println(s);//hello
    }

(21)static String copyValueOf(char[] data, int offset, int count):返回指定数组中表示该字符序列的 String

    @Test
    public void test21() {
        char[] chars = {'h','e','l','l','o'};
        String s = String.copyValueOf(chars,0,2);
        System.out.println(s);//he
    }

(22)static String valueOf(char[] data)  :返回指定数组中表示该字符序列的 String

    @Test
    public void test22() {
        char[] chars = {'h','e','l','l','o'};
        String s = String.valueOf(chars);
        System.out.println(s);//hello
    }

(23)static String valueOf(char[] data, int offset, int count) : 返回指定数组中表示该字符序列的 String

   @Test
    public void test23() {
        char[] chars = {'h','e','l','l','o'};
        String s = String.valueOf(chars,0,2);
        System.out.println(s);//he
    }

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

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

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