ProAnswers.org

How to control string length with precision specifier?

When the precision specifier is applied to strings, the precision specifier specifies the maximum field length. 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 Main {

  public static void main(String args[]) {

    Formatter fmt = new Formatter();

    // Display at most 15 characters in a string.

    fmt = new Formatter();

    fmt.format("%.15s", "12345678901234567890");

    System.out.println(fmt);

 

  }

}

out put

123456789012345