public static void main(String[] args) { String URL = "http://www.baidu/api/v1/posts/%1$s.json?page=%2$d&token=%3$s"; System.out.println(String.format(URL, "info", 11, "sdadasdasd3213213")); //http://www.baidu/api/v1/posts/info.json?page=11&token=sdadasdasd3213213 }
String.format是在JDK1.5中新增的静态方法,它主要功能是格式化数据,大致分为这些类(常规类型、字符类型、数值类型、日期类型)。
它的语法如下: 常规类型、字符类型和数值类型的格式说明符的语法:%[参数索引位置$][转换标识符][最小官渡][.保留精度位数]转换方式,1$, 2$代表参数索引位置,而d, s则是转换标示符,关于更多的转换标示符见下面:
%s 字符串类型 如"name"
%c 字符类型 ‘m’
%b 布尔类型 true
%d 整数类型(十进制) 99
%x 整数类型(十六进制) FF
%o 整数类型(八进制) 77
%f 浮点类型 99.99
%a 十六进制浮点类型 FF.35AE
%e 指数类型 9.38e+5
%g 通用浮点类型(f和e类型中较短的)
%% 百分比类型 %
%n 换行符
%tx 日期与时间类型(x代表不同的日期与时间转换符)
时间: 2024-08-03 15:22:47