2014-01-27 38 views
-1

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

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`。

+0

您是否正在更换|有空格? – Leo

+3

并不是这个问题有关http://stackoverflow.com/questions/21372450/java-splitting-command-line-argument? – Leo

+0

试试这个使用replaceAll(“|”,“”); –

回答

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构建输出。