Format fixed-format text file lines
there's a text file
first second third 1 2 3 yes no ok hmmmmmmm yep_a_long_word_it_is ahahahahahahha
what java functions /libs to use to align words so that they are looked like this (fixed width based on the longest column's length), let's say center align:
first second third 1 2 3 yes 开发者_如何学编程 no ok hmmmmmmm yep_a_long_word_it_is ahahahahahahha
You know, I need "JUSTIFY-FULL" functionality of Microsoft Word (Ctrl + J).
See center(String, int)
method of org.apache.commons.lang.StringUtils
in Apache Commons Lang.
Link: http://commons.apache.org/lang/
I don't know of any libraries that will do this for you, but it is a pretty trivial programming task...
Make one pass through the data and measure the maximum length of a string in each column
Make a second pass through writing the data out with padding on either side of each datum to make the width the same as the maximum.
Write yourself a function that pads a String on either end to a fixed length.
EDIT: just seen the other answer about the Apache commons string centering utility - that'll save you having to write one, so long as you don't mind adding that dependency to you rproject.
精彩评论