字符串

StringBuilder

public class StringBuilderDemo {
    public static void main(String[] args) {
        test();
    }

    static void test() {
        // 需求:使用String完成50000个字符串的拼接: 7640ms
        long begin = System.currentTimeMillis();
        String str = "";
        for (int i = 0; i < 50000; i++) {
            str += i;
        }
        System.out.println(System.currentTimeMillis() - begin); // 7640ms
        System.out.println("---------------");
        // 需求:使用StringBuild完成50000个字符串的拼接/特点:线程安全性较低,但是性能较高     多线程使用StringBuild
        begin = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 50000; i++) {
            sb.append(i);
        }
        System.out.println(System.currentTimeMillis() - begin); // 16ms
        // 需求:使用StringBuffer完成50000个字符串的拼接/特点:线程安全性较高,但是性能较低    单线程使用StringBuffer
        begin = System.currentTimeMillis();
        StringBuffer sbf = new StringBuffer();
        for (int i = 0; i < 50000; i++) {
            sbf.append(i);
        }
        System.out.println(System.currentTimeMillis() - begin); // 0ms
    }
}

String

// String类演示程序
public class StringDemo {
    public static void main(String[] args) {
        // 判断字符串是否非空
        System.out.println(StringUtil.hasLength(null));
        System.out.println(StringUtil.hasLength(""));
        System.out.println(StringUtil.hasLength("    "));
        System.out.println(StringUtil.hasLength("AAA"));
        System.out.println("-------------");
        String str = "ABCDEFG";
        // char charAt(int index) 返回 char指定索引处的值。
        char c = str.charAt(3);
        System.out.println(c); // D
        System.out.println("--------------");
        // String concat(String str) 将指定的字符串连接到该字符串的末尾。
        String str2 = "A".concat("B").concat("C");
        System.out.println(str2); // ABC
        System.out.println("---------------");
        // 判断该字符串是否以指定的字符串开始或结尾
        // boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头。
        // boolean endsWith(String suffix)  测试此字符串是否以指定的后缀结尾。
        System.out.println(str.startsWith("AB")); // true
        System.out.println(str.endsWith("FG")); // true
        System.out.println("------------");
        // 字符串的比较
        // boolean equals(Object anObject) 将此字符串与指定对象进行比较。
        // boolean equalsIgnoreCase(String anotherString) 忽略大小写比较
        System.out.println(str == "ABCDEFG"); // true
        String s1 = new String("ABCDEFG");
        System.out.println(s1 == str); // false
        System.out.println(str.equals(s1)); // true
        System.out.println("AA".equals("aa")); // false
        System.out.println(true); // true
        System.out.println("--------------------");
        // byte[] --> String
        byte[] data = "ABCD".getBytes();
        // String --> byte[]
        String str3 = new String(data);
        System.out.println(str3);
        System.out.println("---------------");
        // int indexOf(int ch) 在该字符串指定字符第一次出现的位置
        // int lastIndexOf(int ch)  返回指定字符的最后一次出现的字符串中的索引。
        System.out.println("AABBCDXFFSAA".indexOf("A")); // 0
        System.out.println("AABBCDXFFSAA".lastIndexOf("A")); // 11
        System.out.println("-----------------");
        // 替换
        // replace(char oldChar, char newChar)
        System.out.println("ABCA".replace("A", "G")); // GBCG
        // 大小写转换
        System.out.println("ADSSxca".toUpperCase()); // ADSSXCA
        System.out.println("ADSSxca".toLowerCase()); // adssxca
        // trim:消除前后空格
        System.out.println(" ABC ".length()); // 5
        System.out.println(" ABC ".trim().length()); // 3
    }
}

StringEquals

public class StringEqualsDemo {
    public static void main(String[] args) {
        String str1 = "ABCD";
        String str2 = "A" + "B" + "C" + "D";
        String c = "CD";
        String str3 = "AB" + c;
        System.out.println(str1 == str2); // true
        System.out.println(str1 == str3); // false
    }
}

StringUtil

public class StringUtil {
    // 不允许随便创建对象
    private StringUtil(){}
    // 字符串即不为null,也不为""
    public static boolean hasLength(String str) {
        return str != null && !"".equals(str.trim());
    }

    // 判断字符串为空(可能为null,可能为"")
    public static boolean isBlank(String str) {
        return !hasLength(str);
    }
}
原文地址:https://www.cnblogs.com/zengqinghong/p/11827995.html