String类的基本原理

来源:互联网 发布:淘宝客服是干嘛的 编辑:程序博客网 时间:2024/05/21 03:20

String类可以说是最常见的类,用途很广。

class Main {    public static void main(String[] args)    {            //method_1();            //method_2();            //method_3();            //method_4();        //method_5();        method_6();    }    public static void method_1()//获取    {        String a = "awersfaadgb";        sop(a.length());        sop(a.charAt(4));        sop(a.indexOf('b'));        sop(a.indexOf('a',3));    }    public static void method_2()//替换    {        String p = "hello java";        //String p1 = p.replace('a','n');//如果替换的字符不存在,那么就返回原串        String p1 = p.replace("hello","nihao");        String p2 = p.replace("java","c++");        // System.out.println("p1 = "+p1);        sop(p1);        sop(p2);    }    public static void method_3()//切割    {        String ss = "zhangsan,lisi,wangwu";        String[] x = ss.split(",");        for(int i = 0;i<x.length;i++)        {            sop(x[i]);        }    }    public static void method_4()//子串    {        String s = "abcdefgh";        String s1 = s.substring(3);        String s2 = s.substring(3,6);//包含头,不包含尾        sop(s1);        sop(s2);    }    public static void method_5()//转换    {        String s = "   Hello java    ";        sop(s.toUpperCase());        sop(s.toLowerCase());        sop(s.trim());    }    public static void method_6()//比较    {        String s1 = "abc";        String s2 = "acc";        sop(s1.compareTo(s2));    }    public static void sop(Object op)  //输出函数,用起来很方便    {        System.out.println(op);    }}