Java从命令行参数格式化字符串

时间:2014-01-27 05:02:17

标签: java string formatting

我需要帮助格式化从命令行参数传入的字符串。所以当我输入

java Main "Tassimo T46 Home Brewing System|43-0439-6|17999|0.30:Moto Precise Fit Rear Wiper Blade|0210919|799|0.0: Easton Stealth Reflex Composite Hockey Stick| 83-4567-0|8999|0.5:Yardworks 4-Ton Log Splitter|60-3823-0|39999|0"

所以应该格式化显示名称后跟几个空格,然后是整数,接着是另外几个空格,然后是整数的第二部分,然后是空格,然后是小数

示例

coffee      123456     199999     0.30

使用String.format`。

1 个答案:

答案 0 :(得分:0)

here开始,您只能使用String.format:

执行此类操作
String.format("%1$s %2$s %2$s %3$s", "a", "b", "c");

和输出:

a b b c

所以我会推荐@ praveen_mohan的回答。使用replaceAll方法,或迭代your_input.split(“|”)并使用StringBuilder构建输出。