JAVAString用法

package Strings;

import java.util.Arrays;

import org.junit.Before;

@SuppressWarnings("unused")
public class Strings {
    @Before
    public static void main(String[] args) {
        String ss = "Hello World";
        String a = "\\w{8,10}@\\w{3}" + ".com";
        String b = "[email protected]";
        System.out.println(b.matches(a));// 正则表达验证
        String sc = ss.replace("Q", "QAQ");// 字符替换
        // ss.replaceFirst("\\d", replacement)
        String sc1 = ss.replaceAll("\\d", "QAQ");// 字符替换
        StringBuilder saa = new StringBuilder("23333");
        saa.insert(saa.length(), 121);
        System.out.println(saa);
        System.out.println(sc + "\n" + sc1);
        System.out.println(sc.length());// 字符长度
        System.out.println("sss".concat(sc));// 末尾追加拼接
        System.out.println("QAQ".contains("AQ"));// 是否指定字符
        System.out.println(sc.endsWith("AQ"));// 测试是否以指定字符串结尾
        System.out.println(ss.equalsIgnoreCase(sc));// 不分大小写比较
        System.out.println(sc.hashCode());// 哈希值
        System.out.println(sc.indexOf("w"));// 查找此字符并返回第一次出现的位置未包含则返回-1
        System.out.println("Hello World".indexOf("Y", 2));// 从指定位置开始查找指定字符串并返回相对查找的起始点的位置
        System.out.println(sc.intern());// 规范化表示形式
        System.out.println(sc.isEmpty());// 长度是否为零
        System.out.println(sc.lastIndexOf("Q"));// 最后一次出现的位置
        System.out.println(sc.offsetByCodePoints(0, 2));// 从给定的 index 处偏�? codePointOffset 个代码点的索�?
        System.out.println(sc.regionMatches(1, "QAQ", 0, 3));// 从QAQ下标1的字符开始与QAQ�?0-3位之�?(AQ&QAQ)的字符比�?
        System.out.println("reuwq".charAt(2));// 取指定位置字符串
        char[] s = ss.toCharArray();// 字符串转字符数组
        // byte[] b = ss.getBytes();// 字符串转byte数组
        // System.out.println(Arrays.toString(s) + Arrays.toString(b));
        System.out.println("  He llo  ".trim());// 去掉左右两边空格
        System.out.println("Hello World".substring(0, 5));
        String[] xx = "Hello World".split(" ");
        System.out.println(xx.length + "-" + xx[0] + "-" + xx[1]);
        System.out.println("Hello World".toUpperCase());// 转大写
        System.out.println("Hello World".toLowerCase());// 转小写
        int in = 12432;
        double db = 1234.5432;
        boolean d = true;
        System.out.println(String.valueOf(in));
        System.out.println(String.valueOf(db));
        System.out.println(String.valueOf(d));

        String str = "Hello World";
        // 字符串转数组两种
        char[] ch = new char[str.length()];
        char[] cc = new char[str.length()];
        char[] xc = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            ch[i] = str.charAt(i);
            cc[i] = str.substring(i, (i + 1)).charAt(0);
        }
        System.out.println(Arrays.toString(ch) + "\n" + Arrays.toString(cc));
        // 字符数组转字符串三种
        String ccc = "";
        String ccd = "";
        StringBuffer cce = new StringBuffer();
        for (char el : cc) {
            ccc += el;
            ccd = ccd.concat(String.valueOf(el));
            cce.append(el);
        }
        System.out.println(ccc);
        System.out.println(ccd);
        System.out.println(cce);
    }
}

你可能感兴趣的:(JAVAString用法)