Java中的String类

String类不可变的字符串对象,尽量少使用+=操作

1.String类常用的方法

public static void main(String[] args) {
        //判断两个字符是否相等
        String a = "ok";
        String b = "ok";
        System.out.println(a.equals(b));//判断的是值
        System.out.println(a == b);//判断的是地址
        System.out.println(a.equalsIgnoreCase(b)); //忽略大小写

        String c = new String("ok");
        String d = new String("ok");
        System.out.println(c.equals(d));//判断的是值
        System.out.println(c == d);//判断的是地址

        //判断字符个数
        System.out.println("hello中国".length());
        //判断字符字节数
        System.out.println("hello中国".getBytes());

        //trim() 清除左边 右边连续空格
        System.out.println("   hello  world  ".trim());  //hello  world

        //replace() 查找替换方法,查找空格,替换为"" 功能是清除所有的空格
        System.out.println("   hello  world  ".replace(" ", "")); //helloworld

        //字符串截取操作
        System.out.println("hello,world".substring(5)); //,world

        //split 将字符串分成组
        String[] st = "java123mysql456pyhton789".split("\\d+"); // \\代表[0-9]
        System.out.println(Arrays.toString(st));  //[java, mysql, pyhton]

        //concat 字符串连接 将前一个字符串和后一个字符串连在一起
        System.out.println("hello".concat("world")); //helloworld
}

2.String类中转义字符常用的方法

        // \b退格
        System.out.println("hello java\b");  //hello jav
        // \n换行
        System.out.println("hello \njava");
        // \r回车(CR) ,将当前位置移到本行开头
        System.out.println("he\rllo java");  //llo java
        // \t(跳到下一个TAB位置)
        System.out.println("hello jav\ta");  //hello jav   a

        //输出 hello,"java",'java17'。
        //代表一个反斜线字符'''
        System.out.println("hello,\"java\",'java17'。");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值