String类常用方法实例


/* String 字符串控制与判断*/
public class Main {
    public static void main(String[] args) {
        // 赋值 字符串 s1 s2 s3
        // nele为空
        String nule="";
        String s1 = "ADD520xwmm";
        String s2 = new String(s1);
        String s3 = new String("drp1314");
        System.out.println("\ts1\t\t\ts2\t\ts3\t   nule");
        System.out.println(s1+" "+s2+" "+s3+" "+nule);

        //1.0 打印字符串长度
        System.out.println("实例1.0:");
        System.out.println(s1.length());//10
        System.out.println(s3.length());//7

        //2.0 比较字符串 内容是否相同 相同真(true)不同假(false) 区分大小写
        System.out.println("实例2.0:");
        System.out.println(s1.equals(s3));//false
        System.out.println(s1.equals(s2));//true

        //3.0 比较字符串长度   s1 <= s2 返回真(true)小于(false) 忽略大小写
        System.out.println("实例3.0:");
        System.out.println(s1.equalsIgnoreCase(s2));//true
        System.out.println(s1.equalsIgnoreCase(s3));//false

        //4.0 字符串拼接 s1=a s2=b  s1=a.concat(s2)  s3=ab;
        System.out.println("实例4.0:");
        System.out.println(s1.concat(s2));//ADD520xwmmADD520xwmm
        System.out.println(s1.concat("adsabb"));//ADD520xwmmadsadd

        //5.0 查找 字符串索引的字符 下标从零开始
        System.out.println("实例5.0:");
        System.out.println(s1.charAt(0));//A
        System.out.println(s1.charAt(3));//5

        //6.0 查找 字符串字符的索引 有就返回索引 没有返回-1
        System.out.println("实例6.0:");
        System.out.println(s1.indexOf('h'));//-1
        System.out.println(s1.indexOf("520"));//3

        //7.0 判断字符串是否为零 是返回true 否返回false
        System.out.println("实例7.0:");
        System.out.println(s1.isEmpty());//false
        System.out.println(nule.isEmpty());//true

        //8.0 判断字符串是否包含传入的字符串 返回真(true)假(false)
        System.out.println("实例8.0:");
        System.out.println(s1.contains(s2));//true
        System.out.println(s1.contains(s3));//false

        //9.0 判断字符串是否以传入字符串开头 返回真(true)假(false)
        System.out.println("实例9.0:");
        System.out.println(s1.startsWith("ADD"));//true
        System.out.println(s1.startsWith("abc"));//false

        //10.0 判断字符串是否以传入字符串结尾 返回真(true)假(false)
        System.out.println("实例10.0:");
        System.out.println(s1.endsWith("mm"));//true
        System.out.println(s1.endsWith("1314"));//false

        //11.0 求字符串中出现某个字符或字符串最后一次出现在字符串中的索引位置 有返回索引没有返回-1
        System.out.println("实例11.0:");
        System.out.println(s1.lastIndexOf('x'));//6
        System.out.println(s1.lastIndexOf("fg"));//-1

        //12.0 传入一个字符串替换前面一个字符串
        System.out.println("实例12.0:");
        System.out.println(s1);
        System.out.println(s1.replace("ADD","drp"));//drp520xwmm

        //13.0 分段字符串 以给出的字符或字符串分两段
        System.out.println("实例13.0:");
        String[] arr = s1.split("520");//注:如遇特殊字符请加(//字符)的格式
        System.out.println(arr[0]);//ADD
        System.out.println(arr[1]);//xwmm

        //14.0 截取字符串 得到一个新的字符串 以索引截取 注:不包括最后一个
        System.out.println("实例14.0:");
        System.out.println(s1.substring(1,6));//DD520
        System.out.println(s1.substring(0,3));//ADD

        //15.0 把字符串中的大写改为小写
        System.out.println("实例15.0:");
        System.out.println(s1);//原字符串 ADD520xwmm
        System.out.println(s1.toLowerCase());//add520xwmm

        //16.0 把字符串中的大写改为大写
        System.out.println("实例16.0:");
        System.out.println(s1);//原字符串 ADD520xwmm
        System.out.println(s1.toUpperCase());//ADD520XWMM

        //17.0 去除调用字符前后空格
        System.out.println("实例17.0:");
        String ss = "   drp520add  ";
        System.out.println(ss);//原字符串 "   drp520add  "
        System.out.println(ss.trim());// "drp520add"

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值