code: public static String rset(String input, int width)
     {
        String result; // result to return
        StringBuilder pad = new StringBuilder();
        if (input == null)
        {
           for (int i = 0; i < width - 1; i++)
           {
              pad.append(' '); // put blanks into buffer
           }
           result = " " + pad; // one short to use + overload
        }
        else
        {
           if (input.length() >= width)
           {
              result = input.substring(0, width); // when input is too long, truncate
           }
           else
           {
              int padLength = width - input.length(); // number of blanks to add
              for (int i = 0; i < padLength; i++)
              {
                 pad.append(' '); // actually put blanks into buffer
              }
              result = pad + input; // concatenate
           }
        }
        return result;
     }
docs: Another method to force an input string into a fixed width field
  and set it on the right with the left side filled with space ' ' characters.
  
  @param input input string
  @param width required width
  @return formatted string