home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Packaging / Extract / StringPad.java < prev   
Encoding:
Text File  |  2000-05-04  |  1.4 KB  |  59 lines

  1. //
  2. // StringPad.java
  3. //
  4. // Performs string padding for outputting to the display
  5. //
  6. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  7. // All rights reserved 
  8. //
  9. final class StringPad
  10. {
  11.     // pads on the right of the string
  12.     static String padPostfix(String string, int desired_length)
  13.     {
  14.         if (string.length() >= desired_length)
  15.         {
  16.             return string;
  17.         }
  18.         else
  19.         {
  20.             char temp[] = new char[desired_length - string.length()];
  21.  
  22.             for (int i = 0; i < temp.length; i++)
  23.                 temp[i] = ' ';
  24.  
  25.             return string + temp;
  26.         }
  27.     }
  28.  
  29.     // pads on the left of the string
  30.     static String padPrefix(String string, int desired_length)
  31.     {
  32.         if (string.length() >= desired_length)
  33.         {
  34.             return string;
  35.         }
  36.         else
  37.         {
  38.             char temp[] = new char[desired_length - string.length()];
  39.  
  40.             for (int i = 0; i < temp.length; i++)
  41.                 temp[i] = ' ';
  42.  
  43.             return temp + string;
  44.         }
  45.     }
  46.  
  47.     // pads on the left of the string
  48.     static String padNumber(int value, int desired_length)
  49.     {
  50.         return padPrefix(Integer.toString(value), desired_length);
  51.     }
  52.  
  53.     // pads on the left of the string
  54.     static String padNumber(long value, int desired_length)
  55.     {
  56.         return padPrefix(Long.toString(value), desired_length);
  57.     }
  58. }
  59.