String 占位符

来源:互联网 发布:cydia显示网络错误 编辑:程序博客网 时间:2024/05/01 12:49

有时候有些字符串其他地方都相同只有某部分需要,改变这时候我们可以考虑一下占位符

public static void main(String[] args) {        String str="hello word:%s%s";        System.out.println(String.format(str,new Object[]{"123","456"}));    }

输出

hello word:123456

我们看一下format的参数列表String java.lang.String.format(String format, Object… args)
Object… args动态参数就是说参数个数不固定所以也可以怎么写

public static void main(String[] args) {        String str="hello word:%s%s";        System.out.println(String.format(str,"123","456"));    }

两种方法的效果是一样的个人比较习惯第一种写法

0 0