Java中String的一些方法用法

   public class String_method {
    public static void main(String[] args) {
        String string = "shadiao";
        String string1 = "laji";
        String string3 = "shadiao♥laji";
        String string5=string5 = "   cacha    ";
        String string4 = "";
        char[] chars = {'1','w','e'};
        Integer integer =4564;


        StringBuffer str = new StringBuffer("dsdcsdcsdc");

        //返回指定索引处的 char 值。
        System.out.println(string.charAt(2));

        //返回指定索引处的字符(Unicode 代码点)。
        System.out.println(string.codePointAt(2));

        //返回指定索引之前的字符(Unicode 代码点)。
        System.out.println(string.codePointBefore(2));

        //返回此 String 的指定文本范围中的 Unicode 代码点数。
        System.out.println(string.codePointCount(0, 2));

        //按字典顺序比较两个字符串,如果参数字符串等于此字符串,则返回值 0;
        // 如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值;
        // 如果此字符串按字典顺序大于字符串参数,则返回一个大于 0 的值。
        System.out.println(string1.compareTo(string));

        //按字典顺序比较两个字符串,不考虑大小写
        System.out.println(string1.compareToIgnoreCase(string));

        // 将指定字符串连接到此字符串的结尾。
        System.out.println(string.concat("♥string1"));

        //将此字符串与指定的 StringBuffer 比较
        System.out.println(string.contentEquals(str));

        //返回指定数组中表示该字符序列的 String。
        System.out.println(chars);

        //当且仅当此字符串包含指定的 char 值序列时,返回 true。
        System.out.println(string.contains("qewq"));

        //startsWith测试此字符串是否以指定的前缀开始。endsWith 测试此字符串是否以指定的后缀结束。
        //
        System.out.println(string.startsWith("l"));
        System.out.println(string1.endsWith("u"));

        //indexOf(char c):返回指定字符在字符串中第一次出现的索引;
        //lastIndexOf(char c):返回指定字符在字符串中最后一次出现的索引;
        System.out.println(string.indexOf("♥"));
        System.out.println(string1.lastIndexOf("♥"));
        System.out.println(string3.indexOf("♥"));

        //根据指定的规则拆分字符串,返回字符串数组,即正则表达式
        System.out.println(string3.split(""));

        //	isEmpty():当且仅当字符串长度为0时,返回true;
        System.out.println(string4.isEmpty());
        System.out.println(string.isEmpty());

        //replace(char old,char new):把字符串中的old字符替换为new字符,然后返回一个新字符串;
        System.out.println(string3.replace("liyageliyage","shadiaoshadiao"));

        //返回字符串的副本,忽略前导空白和尾部空白。
        System.out.println(string5.trim());

        //把指定类型的数据转为字符串类型;
         String.valueOf(2.2);

    }
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值