(Object String 类中的方法练习)

package com.zs.demo1;

public class Demo1 {
    public static void main(String[] args) {
        fun1();
        fun2();
        fun3();
        fun4();
        fun5();
        fun6();
        fun7();
        fun8();
        fun9();
    }

    private static void fun9() {
        // 判断字符串的内容是否相等
        //s.equalsIgnoreCase(s1)  不区分大小写的比较
        System.out.println("方法9:equals()方法");
        String s=new String("hello");
        String s1=new String("hello");
        boolean b=s.equals(s1);
        System.out.println(b);    //输出引用类型String时,输出的不是内存地址,是因为String类重写了toString()方法
    }

    private static void fun8() {
        // 将字符串转字符数组
        System.out.println("方法8:toCharArray()方法");
        String s=new String("与子同袍,岂曰无衣");
        char[] c=s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            System.out.print(c[i]+"  ");    
        }
        System.out.println();
    }

    private static void fun7() {
        // 将字符串转字节数组
        System.out.println("方法7:getBytes()方法");
        String s=new String("与子同袍,岂曰无衣");
        byte[] b=s.getBytes();
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i]+"  ");
        }
        System.out.println();
        String s1=new String(b);//将字节数组转字符串
        System.out.print("字节数组转字符串:");
        System.out.println(s1);
    }

    private static void fun6() {
        //查找一个字符,没有返回-1;indexOf(char ch)只能查找单个字符的下标
        System.out.println("方法6:indexOf()方法");
        String s=new String("hello world");
        int x=s.indexOf("e");
        System.out.println(x);
        
    }

    private static void fun5() {
        // 判断一个一个字符串是否有另一个字符串
        System.out.println("方法5:contains()");
        String s=new String("hello.java");
        boolean b=s.contains("ll");
        System.out.println(b);
    }

    private static void fun4() {
        // 判断一个字符串是否以指定后缀结束
        System.out.println("方法4:endswith()方法");
        String s=new String("hello.java"); 
        boolean a=s.endsWith(".java");
        System.out.println(a);
    }

    private static void fun3() {
        //判断一个字符串是否以指定前缀开始,startswith()方法返回布尔型
        System.out.println("方法3:startswith()方法");
        String s=new String("wwwhello.java");
        boolean b=s.startsWith("hello");//测试此字符串是否以指定的前缀开始
        System.out.println(b);    
        boolean e=s.startsWith("hello",3 );//测试从指定索引的位置开始的字符串是否以指定前缀开始
        System.out.println(e);
        
    }

    private static void fun2() {
        System.out.println("方法2:substring()方法");
        // 获取字符串的一部分,substring();
        //substring(int beginIndex,int endIndex);包含头不包含尾
        String s=new String("fhwuiehfiu");
        String s1=s.substring(1, 3);
        System.out.println(s1);
        //substring(int beginIndex);从开始下标后面的全要
        String s2=s.substring(3);
        System.out.println(s2);
        
    }

    private static void fun1() {
        // int length();返回字符串长度
        String s=new String("dshfiuweh");
        System.out.println("方法1:返回字符串长度");
        System.out.println(s.length());
        
    }
}
/*运行结果:
方法1:返回字符串长度
9
方法2:substring()方法
hw
uiehfiu
方法3:startswith()方法
false
true
方法4:endswith()方法
true
方法5:contains()
true
方法6:indexOf()方法
1
方法7:getBytes()方法
-45  -21  -41  -45  -51  -84  -59  -37  -93  -84  -58  -15  -44  -69  -50  -34  -46  -62  
字节数组转字符串:与子同袍,岂曰无衣
方法8:toCharArray()方法
与  子  同  袍  ,  岂  曰  无  衣  
方法9:equals()方法
true

*/
原文地址:https://www.cnblogs.com/Zs-book1/p/10554872.html