String类常用方法

1 concat 拼接
//方法原型:public String concat (String str)

2 contains 判断是否包含某个小字符串
//方法原型:public boolean contains (CharSequence s)

3 endsWith 是否以xx结尾
//方法原型:public boolean endsWith(String suffix)

4 startsWith 是否以xx开头
//方法原型:public boolean startsWith(String prefix)

5 indexOf 查找目标字符串在当前字符串中第一次出现的索引
//方法原型:public int indexOf(String str)

6 lastIndexOf 查找目标字符串在当前字符串中最后一次出现的索引
//方法原型:public int lastIndexOf(String str)

7 replace 将当前字符串中的目标字符串,替换为另外一个字符串
//方法原型:public String replace(CharSequence s1,CharSequence s2)

8 substring 截取子串
//方法原型:public String substring(int beginIndex)
//方法原型:public String substring(int beginIndex, int endIndex);(含头不含尾)

9 toCharArray 将字符串转成char数组
//方法原型:public char[] toCharArray()

10 toLowerCase 转成纯小写
//方法原型: public String toLowerCase()

11 toUpperCase 转成纯大写
//方法原型:public String toUpperCase()

12 trim 去除字符串两端的空格(不去除空间的空格)
//方法原型:public String trim()

13 split 切割字符串,参数称为切割符
//方法原型:public String[] split(String regex)

public class TestDemo02 {
    public static void main(String[] args) {
        String s = "HelloWorldHelloWorld";
        //1.concat
        String s1 = s.concat("java");
        System.out.println(s1);//HelloWorldHelloWorldjava

        //2.contains
        boolean b1 = s.contains("oWo");
        System.out.println(b1);//true

        //3.StartsWith endsWith
        boolean b2 = s.startsWith("hel");
        boolean b3 = s.endsWith("rld");
        System.out.println(b2);//false
        System.out.println(b3);//true

        //4.indexof  lastIndexof
        int index1 = s.indexOf("oWo");
        System.out.println(index1);//4
        int index2 = s.lastIndexOf("oWo");
        System.out.println(index2);//14
        //5.扩展:如果没有目标字符串返回什么?
        int index3 = s.indexOf("PHP");
        System.out.println(index3);//-1

        //6.replace
        String s2 = s.replace("World","Java");
        System.out.println(s2);//HelloJavaHelloJava

        //7.substing
        String s3 = s.substring(10);
        System.out.println(s3);//HelloWorld
        String s4 = s.substring(10,15);
        System.out.println(s4);//Hello(含头不含尾)

        //8.toCharArray
        char[] chs = s.toCharArray();
        System.out.println(Arrays.toString(chs));
        //[H, e, l, l, o, W, o, r, l, d, H, e, l, l, o, W, o, r, l, d]

        //9.tolowerCase  toUpperCase
        String s5 = s.toLowerCase();
        System.out.println(s5);//helloworldhelloworld
        String s6 = s.toUpperCase();
        System.out.println(s6);//HELLOWORLDHELLOWORLD

        //10.trim
        String ss = " Hello  Java ";
        String s7 = ss.trim();
        System.out.println(s7);//Hello  Java

        //11.split 切割
        String sss = "小花爱小明爱小林爱小薇";
        String[] s8 = sss.split("爱");
        for (String s9 : s8) {
            System.out.println(s9);
        }
        /*小花
          小明
          小林
          小薇*/
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值