Generate fixed length Strings filled with whitespaces

后端 未结 13 943
借酒劲吻你
借酒劲吻你 2020-12-04 13:52

I need to produce fixed length string to generate a character position based file. The missing characters must be filled with space character.

As an example, the fi

相关标签:
13条回答
  • 2020-12-04 14:45

    This code will have exactly the given amount of characters; filled with spaces or truncated on the right side:

    private String leftpad(String text, int length) {
        return String.format("%" + length + "." + length + "s", text);
    }
    
    private String rightpad(String text, int length) {
        return String.format("%-" + length + "." + length + "s", text);
    }
    
    0 讨论(0)
提交回复
热议问题