美文网首页
JavaSE String

JavaSE String

作者: 23b57d72cde7 | 来源:发表于2018-04-10 15:52 被阅读0次
    public class StringTest {
        public static void main(String[] args) {
            // 如何将一个byte数组转换成字符串
            byte[] bs = { 97, 122, 45, 32, 0, 100, 98 };
            String s = new String(bs);
            System.out.println(s);
            // 怎么将String字符串转换成byte数组
            String bb = "张三";
            try {
                // 编码转换问题 GBK->UTF-8
                byte[] bbs = bb.getBytes("UTF-8");
                String r = new String(bbs, "UTF-8");
                System.out.println(r);
                // 将UTF-8 ->GBK
                byte[] bbs2 = r.getBytes("GBK");
                String rr = new String(bbs2, "GBK");
                System.out.println(rr);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            // 返回指定下标的字符
            char c = bb.charAt(1);
            System.out.println(c);
            System.out.println(bb.codePointAt(1));
            String ls = "李四";
            int i = ls.compareTo(bb);
            System.out.println(i);
            // 将李四连接到张三的后面
            String n = bb.concat(ls);
            System.out.println(n);
            // 判断是否拥有此字符串
            System.out.println(bb.contains("李"));
            // 将char数组转换为String字符串
            char cc[] = { '我', '和', '你', '都', '我', '是' };
            String ncc = String.copyValueOf(cc);
            System.out.println(ncc);
            // 判断字符最后一位
            boolean ew = ncc.endsWith("你");
            System.out.println(ew);
            // 忽略大小写的内容比较
            System.out.println(ncc.equalsIgnoreCase(bb));
            // 国际化 i18n zh_CH en_US
            String format = String.format(new Locale("zh_TW"), "张三");
            System.out.println(format);
            // 返回指定字符串的下标
            int indexOf = ncc.indexOf("三");
            System.out.println(indexOf);
            // 从下标3(包含)的位置往后找第一个出现的下标
            int indexOf2 = ncc.indexOf("我", 3);
            System.out.println(indexOf2);
            // 倒着找第一个出现的下标
            int lastIndexOf = ncc.lastIndexOf("我");
            System.out.println(lastIndexOf);
            // 判断字符串是否是空房子
            String nn = "";
            System.out.println(nn.isEmpty());
            // join 特殊拼接
            String join = String.join("#", "张三", "李四", "王五");
            System.out.println(join);
            // 返回字符串的长度
            System.out.println(join.length());
            // 正则表达式
            String tel = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";// 手机号
            String email = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";
            String idCard = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)";
            boolean b = Pattern.compile(tel).matcher("13838389438").matches();
            boolean b1 = Pattern.compile(email).matcher("373828882@qq.com").matches();
            String sfz = "41270119900916353X";
            boolean b2 = sfz.matches(idCard);
            System.out.println(b2);
            // 字符串的替换
            String nnnn = join.replace("#", "@");
            System.out.println(nnnn);
            // 正则表达式进行正则表达式替换
            String replaceAll = join.replaceAll("\\W", "8");
            System.out.println(replaceAll);
            // 只替换第一个
            String reString = join.replaceFirst("#", "8");
            System.out.println(reString);
            // 按照指定正则表达式拆分字符串
            String[] ss = join.split("#");
            System.out.println(Arrays.toString(ss));
            String eg = "张三#20#男@李四#21#男@王五#22#女";
            String[] split = eg.split("@");
            for (String egs : split) {
                String[] split2 = egs.split("#");
                System.out.println(Arrays.toString(split2));
            }
            // 判断字符串以什么开头
            System.out.println(join.startsWith("张"));
            // 截取字符串
            CharSequence cs = join.subSequence(0, 3);
            System.out.println(cs);
            // 截取字符串
            String substring = join.substring(1);
            System.out.println(substring);
            String substring2 = join.substring(2, 5);
            System.out.println(substring2);
            // 截取李四
            String lss = join.substring(join.indexOf("李四"), join.indexOf("李四") + 2);
            System.out.println(lss);
            // 截取三到四
            String substring3 = join.substring(join.indexOf("三"), join.indexOf("四") + 1);
            System.out.println(substring3);
            // 字符串转换为char的数组
            char[] charArray = join.toCharArray();
            System.out.println(Arrays.toString(charArray));
            String xx = "abc";
            // 转大写
            String dxx = xx.toUpperCase();
            System.out.println(dxx);
            // 转小写
            xx = dxx.toLowerCase();
            System.out.println(xx);
            String sn = "     张 三 ";
            // 去空格
            String trim = sn.trim();
            System.out.println(trim);
            // String ->boolean
            String be = "TRUE";
            Boolean valueOf = Boolean.valueOf(be);
            System.out.println(valueOf);
            be = String.valueOf(valueOf);
            System.out.println(be);
    
        }
    
    }
    

    相关文章

      网友评论

          本文标题:JavaSE String

          本文链接:https://www.haomeiwen.com/subject/yjrnhftx.html