java string 占位符_零基础学习JAVA-05.字符串

字符串是程序类最常用的变量,字符串使用String作为类型描述

    public static void main(String[] args) {        String zfc = "我是一个中文字符串";        System.out.println(zfc);    }
1f5e1453d66ef298a799b0b1b585c732.png

其实字符串的核心是一个基本类型里的char[]数字,我们通过点击String查看源码可以发现。

53a8e22a0e245ee6a1b73b3ce1f0a978.png

所以我们也可以获得字符串长度

    public static void main(String[] args) {        String zfc = "我是一个中文字符串";        System.out.println(zfc);        System.out.println(zfc.length());    }
0991109158ad498d2f696f5101af1dfe.png

通过上面的源代码我们发现字符串的核心就是char数组,所以我们就可以对字符串做很多操作,我们先拿常用的举例子:

 public static void main(String[] args) {        String zfc = "abcdefg";        //判断是否以某字符串开头        if(zfc.startsWith("abc")){            System.out.println("zfc以abc开头");        }        //判断是否以某字符串开头        if(zfc.startsWith("cde",2)){            System.out.println("zfc偏移2个字符以cde开头");        }        //判断是否以某字符串结尾        if(zfc.endsWith("efg")){            System.out.println("zfc以efg结尾");        }        //判断字符串是否是指定字符串,建议使用Strings工具栏里的方法        if("abcdefg".equals(zfc)){            System.out.println("zfc是abcdefg");        }        //获得字符串的二进制数组        System.out.println(zfc.getBytes());        //判断字符串是否包含子字符串        if(zfc.contains("abc")){            System.out.println("zfc里面包含子字符串abc");        }        //判断是否包含序列里的字符串        StringBuilder sb = new StringBuilder("abc");        if(zfc.contentEquals(sb)){            System.out.println("zfc里包含sb里面的子字符串");        }        //通过正则表达式匹配字符        if (zfc.matches("abc.*")){            System.out.println("正则匹配到了abc");        }        //分割字符串        String[] array = zfc.split("");        List list = Arrays.asList(array);        list.stream().forEach(e->{            System.out.println("数组元素"+e);        });        //字符串连接,也可以使用+号表示        System.out.println(zfc.concat("hijklmn"));        //查看子字符串子在字符串首次出现的位置,如果未找到返回-1        System.out.println(zfc.indexOf("c"));        //字符串替换        System.out.println(zfc.replace("abc","xyz"));        //截取字符串        System.out.println(zfc.substring(2));    }
7817719ace671b14b7facd4b4e29f6ad.png

字符串连接

由于字符串是char数组形式,所以我们不建议使用concat和"+"这种形式,使用StringBuffer和StringBuilder是一个比较好的方式

    public static void main(String[] args) {        String zfc = "abc";        //StringBuffer是线程安全的,StringBuilder是线程不安全的        //StringBuilder由于不加锁理论上速度要比StringBuffer快        StringBuilder sb1 = new StringBuilder(zfc);        sb1.append("def");        System.out.println(sb1.toString());​        StringBuffer sb2 = new StringBuffer();        sb2.append(zfc);        sb2.append("def");        System.out.println(sb2.toString());    }
b0c50cf4ef2c375bb6f29cfea4dcef6e.png

字符串格式化

    public static void main(String[] args) {        //%d是整数        //%f是浮点数        //%s是字符串        //%n是换行        //%05d 0代表用0补全,5代表是使用5个占位符        String zfc = String.format("整数格式化:%d,浮点数格式化:%f,使用占位符的浮点数格式化:%05d,字符串格式化:%s",1,1.2,3,"hello world");        System.out.println(zfc);        //System.out.printf 主要用户控制台输出        System.out.printf("整数格式化:%d,浮点数格式化:%f,使用占位符的浮点数格式化:%05d,字符串格式化:%s%n",1,1.2,3,"hello world");    }
e43901c13430fe761eb2457d179d779d.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值