初学JAVA——代码练习(字符串)

package zhiZuo4;

public class Test {

    public static void main(String[] args)
    {
        
        //声明定义字符串
        String A = "AAAAAA";
        String B = null;
        B = new String();
        B = new String("AAAAA");
        char[] c = new char[]{'A','A','A'};
        String D = new String(c);
        System.out.println("字符串D长度为"+D.length());
        System.out.println("字符串D为"+D);
        //查找字符或字符串
        int E = D.indexOf("AA");
        System.out.println(E);
        int F = D.indexOf("B");
        System.out.println(F);
        String G = "hdiasdkaenfklajksj";
        int la = G.lastIndexOf("s");
        System.out.println(la);
        //截取字符或字符串
        String H = G.substring(5);
        System.out.println(H);
        String J = G.substring(5, 6);
        System.out.println(J);
        //去除前后空格
        String K = " hu g t eyd ";
        System.out.println(K.trim());
        //查找替换
        System.out.println(K.replace(" ", ""));
        String L = "WWW,WWW,钱已经花光!";
        System.out.println(L.replaceFirst("WWW", "穷死了"));
        //判断字符串的开始和结束
        System.out.println(L.startsWith("W"));
        System.out.println((L.indexOf("W")==0));
        System.out.println(L.endsWith("!"));
        //判断字符串是否相等
        String a = new String("aBc");
        String b = new String("aBc");
        System.out.println((a==b));
        System.out.println((a.equals(b)));
        //大小写转换
        System.out.println(a.toUpperCase());
        System.out.println(b.toLowerCase());
        //字符串分割
        String a2 = "a b c d e f";
        String[] a3 = a2.split(" ");
        for (int i = 0;i<a3.length;i++)
        {
            System.out.print("	"+a3[i]);
        }
    }

}

原文地址:https://www.cnblogs.com/zhaoxifei/p/5052835.html