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

  1. DEFINT A-Z
  2.  
  3. DECLARE FUNCTION Strip$ (orig$, side, char$)
  4.  
  5. FUNCTION Strip$ (orig$, side, char$)
  6. '****************************************************************************
  7. 'Strips leading and/or trailing characters from a string.  It works like
  8. ' LTRIM$() and RTRIM$() but on other characters in addition to spaces.
  9. '
  10. 'The side argument is passed in one of the following ways:
  11. '
  12. '    <0 = Strip the left side
  13. '     0 = Strip both sides
  14. '    >0 = Strip the right side
  15. '
  16. 'Combinations of characters can also be stripped from each side as well as
  17. ' individual characters.  In this case, the length of char$ would be greater
  18. ' than one.  The characters to be stripped ARE case sensitive.
  19. '
  20. 'Examples:     Strip$("00100",-1, "0")  -->  "100"
  21. '              Strip$("AABAa", 0, "A")  -->  "BAa"
  22. '              Strip$("00100", 0, "0")  -->  "1"
  23. '              Strip$("     ", 0, " ")  -->  ""
  24. '              Strip$("ABCDE", 0, "AB") -->  "CDE"
  25. '              Strip$("ABCDE", 1, "AB") -->  "ABCDE"
  26. '
  27. '****************************************************************************
  28.  
  29. IF orig$ = "" OR char$ = "" THEN        'If either argument is a null string,
  30.      Strip$ = orig$                     'return the original because nothing
  31.      EXIT FUNCTION                      'can be stripped from a null string,
  32. END IF                                  'and you can't search for a null.
  33.  
  34. l = LEN(char$)                          'Find the length of the char(s) to
  35.                                         'strip.
  36.  
  37. new$ = orig$                            'Don't alter the original.
  38.  
  39. IF side <= 0 THEN                       'Strip the left side.  Notice how
  40.      DO WHILE LEFT$(new$, l) = char$    'both sides will get stripped if side
  41.           new$ = MID$(new$, l + 1)      'is equal to zero.
  42.      LOOP
  43. END IF
  44.  
  45. IF side >= 0 THEN                       'Strip the right side.
  46.      DO WHILE RIGHT$(new$, l) = char$
  47.           new$ = LEFT$(new$, LEN(new$) - (l + 1))
  48.      LOOP
  49. END IF
  50.  
  51. Strip$ = new$                           'Return the stripped string.
  52.  
  53. END FUNCTION
  54.  
  55.