String类的常用方法

关于String类的构造方法
  1. String s = new String(”“);
  2. String s = ”“; 最常用
  3. String s = new String (char数组)
  4. String s = new String ( char数组,起始下标,长度)
  5. String s = new String(byte数组)
  6. String s = new String(byte数组,起始下标,长度)
    代码如下:
        // 这里只掌握常用的构造方法
        // 创建字符串对象最常用的一种方式
        String s1 = "hello world!";
        // s1当中保存的是一个内存地址,按理说应该输出一个地址
        // 但是输出一个字符串,说明String类已经重写了toString方法
        System.out.println(s1);
        // 这里只掌握最常用的构造方法
        byte[] bytes = {97,98,99}; // 97是a,98是b,99是c(byte数组会自动进行这些转换)
        String s2 = new String(bytes); // 传一个byte数组
        // 前面说过:输出一个引用的时候,会自动调用toString()方法
        // 默认Object的话,会自动输出对象的内存地址
        // 通过输出结果我们得出一个结论: String类已经重写了toString()方法
        // 输出字符串对象的话,输出的不是对象的内存地址,而是字符串本身
        System.out.println(s2); // abc
        // String(字节数组,数组元素下标的起始位置,长度)
        // 将byte数组中的一部分转换成字符串
        String s3 = new String(bytes,1,2);
        System.out.println(s3); // bc
        // 将char数组全部转换成字符串
        char[] chars = {'我','是','中','国','人'};
        String s4 = new String(chars);
        System.out.println(s4);
        // 将char数组的一部分转换成字符串
        String s5 = new String(chars,2,3);
        System.out.println(s5);
        String s6 = new String("hello world!");
        System.out.println(s6);
关于String类的常用方法

String类的方法在业务中经常使用,建议熟练掌握,但是也可以通过查询API文档来解决!

public class StringTest05 {
    public static void main(String[] args) {
        // String类当中的常用方法
        // charAt(int index) // 放回指定索引处的char值
        char c = "中国人".charAt(1); // "中国人"是一个字符串string对象。只要是对象就能"."
        System.out.println(c); // 国

        // 2 (了解) .int compareTo(String anotherString)
        // 按照字典顺序比较
        int result = "abc".compareTo("abc");
        System.out.println(result); //0(等于0) 前后一致

        int result2 = "abcd".compareTo("abce");
        System.out.println(result2); //-1(小于0)前小后大

        int result3 = "abce".compareTo("abcd");
        System.out.println(result3); //1(小于0)前大后小

        // 拿着字符串第一个字母和后面字符串的第一个字符串字母比较。能否胜负就不再比较了。
        System.out.println("xyz".compareTo("yxz")); // -1

        // 3 (掌握) .boolean contains(CharSequence s)
        System.out.println("hello world".contains("hello"));

        // 4(掌握) boolean endsWith()
        // 判断当前字符串是否以某个字符串结尾
        System.out.println("test.txt".endsWith(".java")); // false
        System.out.println("test.txt".endsWith("xt")); // true

        // 5(掌握) .boolean equals(Object object)
        // 比较两个字符串必须使用equals方法,不能使用"=="
        // 底层比较原理是将字符一个一个取出来进行比较
        // equals方法只能看出来是否相等
        // compareTo方法还能看出来谁大谁小
        System.out.println("abc".equals("abc"));

        // 6.equalsIgnoreCase(String anotherString) 判断两个字符串是否相等忽略大小写
        System.out.println("Abc".equalsIgnoreCase("abc"));
        // 7.(掌握).byte[] getBytes()
        // 将字符串对象转换成字节数组
        byte[] bytes = "abcdef".getBytes();
        for (byte s:
             bytes) {
            System.out.println(s);
        }
        // 8.indexOf(int ch)
        //返回指定字符第一次出现的字符串内的索引。
        System.out.println("helloworld".indexOf("world"));

        // 9.判断某个字符串是否为空 .boolean isEmpty() 底层源码应该是调用的是字符串的length
        System.out.println("   ".isEmpty());
        // 10.int length()
        // 面试题: 判断数组长度和判断字符串长度不一样
        // 判断数组长度是length属性,判断字符串长度是length()方法
        System.out.println("abc".length());

        // 11.lastIndexOf(int ch) 返回某个子字符串在当前字符串中最后一次出现位置的索引(下标)
        System.out.println("hellohelloworldwor;ld".lastIndexOf("wor"));

        // 12.replace(CharSequence target, CharSequence replacement)
        // 在当前字符串中用replacement替换掉当前字符串中的target
        // String的父接口就是: CharSequence
        String newString = "http://www.baidu.com".replace("http://", "https://");
        System.out.println(newString);
        // 13.String[] split(String regex)
        // 拆分字符串
        String[] split = "1980-10-11".split("-"); // 以 - 为分隔符拆分字符串
        for (String s:
             split) {
            System.out.println(s);
        }

        // 14.startsWith(String prefix)
        // 判断当前字符串是否以某字符串开始
        System.out.println("hello world".startsWith("hello")); // true
        System.out.println("hello world".startsWith("hellh")); // false

        // 15.substring(int beginIndex) // 参数是起始下标
        // 截取字符串
        System.out.println("https://www.baidu.com".substring(7)); // 从索引为7的位置处开始截取字符串

        // 16.String substring(int beginIndex,int endIndex) 开始下标和结束下标
        // 截取字符串
        System.out.println("https://www.baidu.com".substring(8,15)); // 左闭右开

        // 17.char[] toCharArray()
        // 将字符串转换成char数组
        char[] chars = "我是中国人".toCharArray();
        for (char s1:chars){
            System.out.println(s1);
        }

        // 18.String toLowerCase()
        // 将字符串转换成小写
        System.out.println("HELLO WORLD".toLowerCase());

        // 19.String toUpperCase()
        // 将字符串转换成大写
        System.out.println("hello world".toLowerCase());

        // 20.String trim()
        // 去除字符串 前后 的空白
        System.out.println("  hello  world   ".trim());
        // .String中只有一个方法是静态的,不需要new对象
        // 21.这个方法叫valueOf   作用: 将”非字符串“转换成"字符串"
        String s1 = String.valueOf(true);
        System.out.println(s1);

        String s2 = String.valueOf(new Customer());
        System.out.println(s2); // 没有重写toString方法之前打印的是对象内存地址,因为调用的toString方法是父类Object类的toString方法

        // 研究一下println()方法的源码
        System.out.println(100);
        System.out.println(3.14);
        System.out.println(true);

        Object o = new Object();
        // 通过源代码可以看出: 为什么输出一个引用的时候,会调用toString()方法
        // 本质上System.out.println()这个方法  本质上都是先将目标转换成字符串再输出
        System.out.println(o);

    }
}
class Customer{ // 没有重写toString方法 会调用Object类的toString方法 打印的就是地址

    @Override
    public String toString() {
        return "Customer{}";
    }
}
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鹤林村菠萝皮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值