String中常用的API(二)

本文详细介绍了Java中String类的一些常用方法,包括contains()、indexOf()、lastIndexOf()、substring()以及charAt()等,通过实例展示了如何在字符串操作中运用这些方法进行查找、截取和转换。此外,还涵盖了将字符数组转化为String对象的方法如valueOf()和copyValueOf()。
摘要由CSDN通过智能技术生成

学习整理之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
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zwjStart

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

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

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

打赏作者

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

抵扣说明:

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

余额充值