Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - truncate string from left with formatter flag

I have a string, say:

String s = "0123456789";

I want to pad it with a formatter. I can do this two ways:

String.format("[%1$15s]", s); //returns [     0123456789]

or

String.format("[%1$-15s]", s); // returns [0123456789     ]

if I want to truncate text I do

String.format("[%1$.5s]", s);  // returns [01234]

if I want to truncate from 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

as well as this:

String.format("[%1$.-5s]", s); // throws UnknownFormatConversionException

So how then do I truncate from the left using a format flag?

like image 280
Mark W Avatar asked Sep 02 '14 18:09

Mark W


People also ask

How to truncate a string in Java?

Truncating a String Using the JDK Java provides a number of convenient ways to truncate a String. Let's take a look. 2.1. Using String's substring () Method The String class comes with a handy method called substring . As the name indicates, substring () returns the portion of a given String between the specified indexes.

How to format a string in Java?

How to Use the Formatter Remember C's printf? Formatting a String in Java feels very similar. The format () method of the Formatter is exposed via a static method from the String class. This method accepts a template String and a list of arguments to populate the template with:

How to format string by default locale in Java?

The java string format () method returns the formatted string by given locale, format and arguments. If you don't specify the locale in String.format () method, it uses default locale by calling Locale.getDefault () method.

What happens if we don't specify locale in string format () method?

If you don't specify the locale in String.format () method, it uses default locale by calling Locale.getDefault () method. The format () method of java language is like sprintf () function in c language and printf () method of java language. locale : specifies the locale to be applied on the format () method. format : format of the string.


2 Answers

I hope this is what you need:

System.out.println("'" + String.format("%-5.5s", "") + "'");
System.out.println("'" + String.format("%-5.5s", "123") + "'");
System.out.println("'" + String.format("%-5.5s", "12345") + "'");
System.out.println("'" + String.format("%-5.5s", "1234567890.....") + "'");

output length is always 5:

'     ' - filled with 5 spaces
'123  ' filled with 2 spaces after
'12345' - equals
'12345' - truncated

in addition:

System.out.println("'" + String.format("%5.5s", "123") + "'");

output:

'  123' filled with 2 spaces before

like image 197
Dmitry Sokolyuk Avatar answered Oct 16 '22 17:10

Dmitry Sokolyuk


The - flag is for justification and doesn't seem to have anything to do with truncation.

The . is used for "precision", which apparently translates to truncation for string arguments.

I don't think format strings supports truncating from the left. You'll have to resort to

String.format("[%.5s]", s.length() > 5 ? s.substring(s.length()-5) : s);
like image 9
aioobe Avatar answered Oct 16 '22 18:10

aioobe