Java 常用类和对象 - 字符串

// TODO 字符串:连续字符组合形成的数据整体
        //  java.lang.String
        //  字符串,字符,字节的关系
//        String name = "zhangsan";
//        String name2 = "zhangsan";
//        String name3 = "zhangsan";
//        String name4 = "zhangsan";
//        String name1 = new String("zhangsan");
//        char[] cs = {'a','中','国'};
//        byte[] bs = {-28,-72,-83,-27,-101,-67};
//
//        String s = new String(cs);
//        String s1 = new String(bs,"UTF-8");
//
//        System.out.println(s);
//        System.out.println(s1);
        String s = "\"";
        // 转义字符: \n" => 文字内容的双引号
        // \', \t , \n , \\
        System.out.println(s);
        System.out.println("\'");
        System.out.println("a\tb");
        System.out.println("c\nd");
        System.out.println("e\\f");
    }
}


        // TODO 拼接:将多个字符串连接在一起
//        String s = "a" + "b"; // "ab"
//        String s1 = "ab"; // "ab"
//        String s2 = "abc" + 1 + 2 ;  // abc12
//        String s3 = 1 + "abc" + 2 ;  // 1abc2
//        String s4 = 1 + 2 + "abc";  // 3abc
//
//        System.out.println(s.hashCode());
//        System.out.println(s1.hashCode());
//        System.out.println(s2);
//        System.out.println(s3);
//        System.out.println(s4);
//
//        System.out.println(s1.concat("abc"));

        // TODO 字符串的比较
        String a = "a";
        String b = "A";

        // 相等
//        System.out.println(a.equals(b));
//        // 忽略大小写的相等
//        System.out.println(a.equalsIgnoreCase(b));

        // i = 正数, a > b
        // i = 负数, a < b
        // i = 0, a = b

//        byte b1 = (byte)'A';
//        byte b2 = (byte)'a';
//        System.out.println("A:" + b1);
//        System.out.println("a:" + b2);

//        int i = a.compareTo(b);
//        System.out.println(i);

        // 忽略大小写的比较
//        int i = a.compareToIgnoreCase(b);
//        System.out.println(i);

        // TODO 字符串的截断操作 : 截取字符串的操作
        //String s  = "    Hello World    ";
        // 子字符串
        // substring 方法用于截取字符串,需要传递两个参数,
        //    第一个参数表示截取字符串的起始位置(索引,包含)
        //    第二个参数表示截取字符串的结束位置(索引,不包含)
        //System.out.println(s.substring(0, 11));
        //System.out.println(s.substring(0, "Hello".length()));
        //System.out.println(s.substring(6, s.length()));
        // substring 方法如果值传递一个参数,那么表示从指定位置开始截取字符串,然后截取到最后
        //System.out.println(s.substring(0));

        // 分解字符串: 根据指定的规则对字符串进行分解,可以将一个完整的字符串分解成几个部分。
//        String[] s1 = s.split(" ");
//        System.out.println(s1.length);
//        for (String s2 : s1) {
//            System.out.println(s2);
//        }

        // TODO trim 方法 :去掉字符串的首位空格的意思
        //System.out.println("!" + s.trim() + "!");

        // TODO 字符串的替换
        //String s = "Hello World zhangsan";
        // 替换
        //System.out.println(s.replace("World", "Java"));
        // replaceAll 按照指定的规则去替换
        //System.out.println(s.replaceAll("World|zhangsan", "Java"));

        // TODO 字符串大小写转换
//        String s = "Hello World";
//
//        System.out.println(s.toLowerCase());
//        System.out.println(s.toUpperCase());
//        String className = "user"; // u + ser
//
//        String s1 = className.substring(0,1);
//        String s2 = className.substring(1);
//
//        System.out.println(s1.toUpperCase().concat(s2));
        
        // TODO 字符串的查找
        String s = "World Hello World";
//        char[] chars = s.toCharArray();
//        System.out.println(chars);
//        byte[] bytes = s.getBytes("UTF-8");
//        System.out.println(bytes);
//        // charAt 可以传递索引定位字符串中指定位置的字符
//        System.out.println(s.charAt(1));
        // indexOf 用于获取数据在字符串中第一次出现的位置
        //System.out.println(s.indexOf("World"));
        // lastIndexOf 用于获取数据在字符串中最后一次出现的位置
        //System.out.println(s.lastIndexOf("World"));

        // 是否包含指定的字符串,返回布尔类型
        System.out.println(s.contains("Hello World"));
        // 判断字符串是否以指定数据开头,返回布尔类型
        System.out.println(s.startsWith("World"));
        // 判断字符串是否以指定数据结尾,返回布尔类型
        System.out.println(s.endsWith("World"));
        // 判断字符串是否为空,空格其实是一个特殊的字符,所以看不到,但是不为空
        System.out.println(s.isEmpty());
    }
}

   // StringBuilder : 构建字符串
//        String s = "";
//        for ( int i = 0; i < 100; i++) {
//            s += i ;
//        }
//        System.out.println(s);
//        StringBuilder s = new StringBuilder();
//        for ( int i = 0; i < 100; i++) {
//            s.append(i);
//        }
//        System.out.println(s.toString());
        StringBuilder s = new StringBuilder();
        s.append("abc");
        System.out.println(s.toString());
        System.out.println(s.length());
        System.out.println(s.reverse());   // 反转字符串
        System.out.println(s.insert(1, "d"));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值