Data Type Java Tutorial

For example, %5.7s displays a string at least five and not exceeding seven characters long.
If the string is longer than the maximum field width, the end characters will be truncated.

import java.util.Formatter;
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt;
    fmt = new Formatter();
    fmt.format("%5.7s", "abcdefghijklmn");
    System.out.println(fmt);
    
    fmt = new Formatter();
    fmt.format("%5.7s", "abc");
    System.out.println(fmt);
    
  }
}
abcdefg
abc