home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / homonlib.zip / JUSTIFY.BAS < prev    next >
BASIC Source File  |  1995-04-13  |  2KB  |  55 lines

  1. DEFINT A-Z
  2.  
  3. DECLARE FUNCTION Justify$ (orig$, side)
  4.  
  5. FUNCTION Justify$ (orig$, side)
  6. '****************************************************************************
  7. 'Moves leading or trailing spaces to the appropriate side of the string,
  8. ' while retaining the original length of the string.
  9. '
  10. 'The side argument can take one of the following forms:
  11. '
  12. '    <0  =  Left justify   (move leading spaces to the right side)
  13. '     0  =  Center justify (spread spaces evenly on both sides)
  14. '    >0  =  Right justify  (move trailing spaces to the left side)
  15. '
  16. 'Feel free to create some constants so these values easier to remember.
  17. '
  18. 'The function works by comparing the size of the original string to the size
  19. ' of the string after trimming the appropriate spaces.  These spaces are then
  20. ' tacked back on to the appropriate side.
  21. '
  22. 'Examples:  Justify$("Some text    ", 0)  -->  "  Some text  "
  23. '           Justify$("Some more    ", 1)  -->  "    Some more"
  24. '           Justify$("   Even more!",-1)  -->  "Even more!   "
  25. '
  26. '****************************************************************************
  27.  
  28. l = LEN(orig$)                          'Get the original length.
  29.  
  30. SELECT CASE side
  31.      CASE IS < 0                        'Left justify
  32.           new$ = LTRIM$(orig$)
  33.           n = l - LEN(new$)
  34.           new$ = new$ + SPACE$(n)
  35.      CASE IS > 0                        'Right justify
  36.           new$ = RTRIM$(orig$)
  37.           n = l - LEN(new$)
  38.           new$ = SPACE$(n) + new$
  39.      CASE ELSE                          'Center justify
  40.           new$ = LTRIM$(RTRIM$(orig$))
  41.           n = l - LEN(new$)
  42.           FOR l = 1 TO n                     'Stick the spaces back onto
  43.                IF l MOD 2 = 0 THEN           'alternating sides until we're
  44.                     new$ = " " + new$        'back to the original length.
  45.                ELSE
  46.                     new$ = new$ + " "
  47.                END IF
  48.           NEXT l
  49. END SELECT
  50.  
  51. Justify$ = new$                         'Return the justified string.
  52.  
  53. END FUNCTION
  54.  
  55.