java常用类一

列举一些string常用类的使用 

String:

1、

public class Home1 {

    public static void main(String[] args) {

        byte[] bytes = {97,98,99};
        String s = new String(bytes);
        String s1 = new String(bytes,0,2);//从0开始,长度取2,只取了前面两个字符
        System.out.println(s);
        System.out.println(s1);

    }

}

/2

 2、equals

public class Home2 {

    public static void main(String[] args) {

        String s = "abc";
//        String b = "a b c";
        String b = "ABC";
//        System.out.println(s.length());//打印长度
//
//        System.out.println(s.concat("bbb"));

        System.out.println(s.equalsIgnoreCase(b));//忽略大小写,比较两个字符串一模一样,常用于验证码使用
        System.out.println(s.equals(b));//比较两个字符串一模一样

    }

}

3、 charAt

public class Home3 {

    public static void main(String[] args) {

        String s = "abc";
        String b = "aBc";

        //char charAt(int index)截取某个字符
        s.charAt(0);
        s.charAt(2);
        System.out.println(s.charAt(0));
        System.out.println(s.charAt(2));

    }

}

4、indexOf

public class Home4 {

    public static void main(String[] args) {
        String s = "abcdefcd";

        System.out.println(s.indexOf("cd"));//找到cd,如果匹配,返回第一个字母的下标,多个cd,只会返回第一个
        System.out.println(s.indexOf("de"));
        System.out.println(s.indexOf("df"));//没找到,返回-1

        System.out.println(s.lastIndexOf("cd"));//从后往前找

    }


}

 

 5、

public class Home5 {

    public static void main(String[] args) {

        String s = "abcdefg";
        System.out.println(s.startsWith("bc"));
        System.out.println(s.startsWith("ab"));//开始有没有这个,有输出true
        System.out.println(s.endsWith("fg"));//结束有没有这个


    }

}

 6、compareTo

public class Home6 {

    public static void main(String[] args) {

        String str1 = "strings";
        String str2 = "strings";
        String str3 = "stringsabc";

        int result = str1.compareTo(str2);//str1和str2比较,0个不同
        System.out.println(result);

        result = str2.compareTo(str3);//str2和str3比较,str2比str3小,返回-3个不同
        System.out.println(result);

        result = str3.compareTo(str2);//str3和str2比较,str3比str2打,返回3个不同
        System.out.println(result);


    }

}

 7、substring

public class Home7 {

    public static void main(String[] args) {

        String s = "abcderf";
        System.out.println(s.substring(0,5));//从第0到第5个,包括5
        System.out.println(s.substring(1,5));//从第一个到第5个之前,不包括5


    }

}

8、trim

public class Home8 {

    public static void main(String[] args) {

        String s = " ab cde fg ";
        System.out.println(s.trim());//把前面和后面的空格去掉,中间不去


    }

}

 

9、 replaceAll

public class Home9 {

    public static void main(String[] args) {

        String s = "aaabbbcccbbbaaaccc";
        s = s.replaceAll("b", "h");//把所有的b换成h
        System.out.println(s);

    }


}

 10、split

public class Home10 {

    public static void main(String[] args) {


        String ss = "1,2,3,4,5";
        String[] split = ss.split(",");
        for (String s : split){

            System.out.println(s);
        }

    }
}

11、 

public class StringDemo {

    public static void main(String[] args) {

        String s = "abc";
        char[] c = {'a', 'b', 'c'};
        //在java里面写代码两个字符串相加,效率比较低,原理是底层是两个数组相加
        //到了jdk1.8,代码是这种写法,在编译过程中会转换成下面高效率那种,所有jdk1.8两种效率一样
        String a = "aaa";
        String b = "bbb";
        String str = a + b;


        //这种方法直接相加,机器效率高,在jdk1.8之前,使用
        StringBuilder sb = new StringBuilder();
        sb.append("aaa");
        sb.append("bbb");
        String s1 = sb.toString();
        System.out.println(sb);


    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值