18、String类的方法

public class StringTest01 {
    
    
    public static void main(String[] args) {
    
    
        //String类中常用方法
        //1(掌握)、char(返回的值的类型) charAt(int index)
        //取出字符串中第index位置的字符
        char c = "中国人".charAt(1);//“中国人”字符串是一个String对象,只要对象就能“点”
        System.out.println(c);//国

        //2(了解)、int compareTo(String anotherString)
        //字符串之间比较大小不能直接使用“<”、“>”,需要使用compareTo方法
        int result1 = "abc".compareTo("abc");//0 (等于0) 前后一致,   10-10 =0
        System.out.println(result1);

        int result2 = "abcd".compareTo("abce");//-1(小于0) 前小后打    8-9 =-1
        System.out.println(result2);

        int result3 = "abce".compareTo("abcd");//1(大于0)  前大后小   9-8 =1
        System.out.println(result3);

        //拿着字符串和后面的字符串进行比较,能分出胜负就不再比较
        System.out.println("xyz".compareTo("yxz"));//-1

        //3(掌握)、boolean contains(CharSequence s)
        //判断前面的字符串是否包含后面的子字符串
        System.out.println("helloworld.java".contains(".java"));//true
        System.out.println("http://www.baidu.com".contains("https"));//false

        //4(掌握)、boolean endsWith(String suffix)
        //判断当前字符串是否以某个字符串结尾
        System.out.println("test.txt".endsWith(".java"));//false
        System.out.println("test.txt".endsWith(".txt"));//true
        System.out.println("ajklflkdsjfkljsfklfjsdflkjss".endsWith("ss"));//true

        //5(掌握)、 boolean equals(Object anObject)
        //比较两个字符串必须使用equals方法,不能使用"=="
        //equals方法有没有调用compareTo方法?老版本可以看一下,JDK13并没有调用compareTo方法
        //equals只能看出相等不相等
        //compareTo方法可以看出是否相等,并且同时还可以看出谁大谁小
        System.out.println("abc".equals("abc"));//true

        //6(掌握)、 boolean equalsIgnoreCase(String anotherString)
        //判断两个字符串是否相等,并且同时忽略大小写
        System.out.println("ABc".equalsIgnoreCase("abc"));//true
    }
}

public class StringTest01 {
    
    
    public static void main(String[] args) {
    
    

        //7(掌握)、 byte[] getByte()
        //将字符串对象转换成字节数组
        byte[] bytes = "abcdef".getBytes();
        for (int i = 0;i<bytes.length;i++){
    
    
            System.out.println(bytes[i]);
        }
        //97
        //98
        //99
        //100
        //101
        //102

        //8(掌握)、 int indexOf(String str)
        //判断某个子字符串在当前字符串中第一次出现的索引(下标)
        System.out.println("oraclejavac++.netc#phppythonjavaoraclec++".indexOf("java"));//6


        //9(掌握)、 boolean isEmpty()
        //判断某个字符串是否为“空字符串”,底层代码应该是字符串的Length()方法
        String s = "a";
        System.out.println(s.isEmpty());//false


        //10(掌握)、int length()
        //面试题:判断数组长度和判断字符串长度不一样
        //判断数组长度是length属性,判断字符串长度是length()方法。
        System.out.println("abc".length());//3

        System.out.println("".length());//0


        //11(掌握)、 int lastIndexOf(String str)
        //判断某个子字符串中最后一次出现的索引(下标)
        System.out.println("oraclejavac++javac#phpjavapython".lastIndexOf("java"));//22


        //12(掌握)、 String replace(CharSequence target,CharSequence replacement)
        //替换
        //String的父接口就是:CharSequence
        String newString = "http://www.baidu.com".replace("http://","https://");
        System.out.println(newString);//https://www.baidu.com
        //把以下字符串中的“=”替换成“:”
        String newString2 = "name=zhangsan&password=123&age=20".replace("=",":");
        System.out.println(newString2);//name:zhangsan&password:123&age:20


        //13(掌握)、 String[] split(String regex)
        //拆分字符串
        String[] ymd = "1998-10-11".split("-");//1998-10-11以-分隔开
        for (int i = 0; i < ymd.length; i++) {
    
    
            System.out.println(ymd[i]);
        }
            //1998
            //10
            //11

        String param = "name=zhangsan&password=123&age=20";
        String [] params = param.split("&");
        for (int i = 0; i < params.length; i++) {
    
    
            System.out.println(params[i]);
        }
            //name=zhangsan
            //password=123
            //age=20

        //14(掌握)、boolean startWith(String prefix)
        //判断字符串是否以某个子符串开始
        System.out.println("http://www.baidu.com".startsWith("http"));//true
        System.out.println("http://www.baidu.com".startsWith("https"));//false


        //15(掌握)、String substring(int beginIndex)(参数是起始下标)
        //截取字符串
        System.out.println("http://www.baidu.com".substring(7));//www.baidu.com


        //16(掌握)、String substring(int beginIndex,int endIndex)
        //beginIndex起始位置(包括)
        //endIndex结束位置(不包括)
        System.out.println("http://www.baidu.com".substring(7,10));//www


        //17(掌握)、 char[] toCharArray()
        //将字符串转换为char数组
        char[] chars = "我是中国人".toCharArray();
        for (int i = 0; i < chars.length; i++) {
    
    
            System.out.println(chars[i]);
        }
        //www
        //我
        //是
        //中
        //国
        //人



        //18(掌握)、String toLowerCase()
        //改为小写。
        System.out.println("ABCDefkXYz".toLowerCase());//abcdefkxyz

        //19(掌握)、String toUpperCase()
        //改为大写
        System.out.println("ABCDefkXYz".toUpperCase());//ABCDEFKXYZ

        //20(掌握)、 String trim()
        //去除字符串前后的空白
        System.out.println("        hello   world           ".trim());//hello   world


        //21(掌握)、String中只有一个方法是静态的,不需要new对象
        //这个方法叫做valueOf
        //作用:将“字符串”转换为字符串
        String s1 = String.valueOf(true);
        System.out.println(s1);//字符串true


    }




}

猜你喜欢

转载自blog.csdn.net/Alopecian/article/details/115303784