I have a line, say:
String s = "0123456789";
I want to add it to the formatter. I can do this in two ways:
String.format("[%1$15s]", s); //returns [ 0123456789]
or
String.format("[%1$-15s]", s); // returns [0123456789 ]
if i want to crop the text i do
String.format("[%1$.5s]", s); // returns [01234]
If I want to truncate on the left, I thought I could do this:
String.format("[%1$-.5s]", s); // throws MissingFormatWidthException
but this failed, so I tried this:
String.format("[%1$-0.5s]", s); // throws MissingFormatWidthException
and:
String.format("[%1$.-5s]", s); // throws UnknownFormatConversionException
So, how then do I crop to the left using the format flag?
java string-formatting
Mark w
source share