[Solved] Printing out box of Hashtags(#) surrounding text


You can use System.out.printf:

Let’s consider String str = "012345678901234567890123456789012345678";

  • add spaces before the string:

    System.out.printf("%56s", str);
    

    Output:

    ############################################################
    #                                                          #
    #                  012345678901234567890123456789012345678 #
    #                                                          #
    ############################################################
    
  • add spaces after the string:

    System.out.printf("%-56s", str);
    

    Output:

    ############################################################
    #                                                          #
    # 012345678901234567890123456789012345678                  #
    #                                                          #
    ############################################################
    

To be sure that the string is not too long, you can use the substring method.

E.g.:

  • when you assign a value to str, just call the subtring method if its length is greater than 56 – str=something; becomes str = something.length() > 56 ? something.substring(0, 56) : something;

7

solved Printing out box of Hashtags(#) surrounding text