我正在使用printf()在Java中创建输出来创建表头.其中一列需要可变宽度.
基本上它应该是这样的:
//two coords
Trial Column Heading
1 (20,30)(30,20)
//three coords
Trial Column Heading
1 (20,40)(50,10)(90,30)
Run Code Online (Sandbox Code Playgroud)
我试着用:
int spacing = numCoords * 7; //size of column
printf("Trial %*^s", column, "Column Heading");
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在转换语句中使用*或^时,我一直收到输出错误.
有谁知道正确的格式字符串应该是什么?
使用Commons Lang库中的 StringUtils.center :
StringUtils.center(column, "Column Heading".length());
Run Code Online (Sandbox Code Playgroud)