udfStrStrip (1)
str udfStrStrip (str, str, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrStrip (strString, strStripChars, strBoundary)
If strString == "" Then Return strString
If strStripChars == "" Then Return strString

strDelim = Num2Char (1) ; Helper char for space char trimming.

strBoundary = StrLower (StrSub (strBoundary, 1, 1))
If !StrScan (strBoundary, "lrb", 1, @FWDSCAN) Then strBoundary = "b"
intBound = StrIndex ("lrb", strBoundary, 1, @FWDSCAN)

While @TRUE
   strBoundaryChars = ""
   Switch intBound
   Case 1
   Case 3
      strBoundaryChars = StrSub (strString, 1, 1)
      Continue
   Case 2
   Case 3
      strBoundaryChars = StrCat (strBoundaryChars, StrSub (strString, StrLen (strString), 1))
      Break
   EndSwitch
   intPos = StrScan (strStripChars, strBoundaryChars, 0, @FWDSCAN)
   If intPos == 0 Then Break
   strChar = StrSub (strStripChars, intPos, 1)

   Switch @TRUE
   Case strChar == " "
      Switch intBound
      Case 1
         strString = ItemExtract (1, StrTrim (StrCat (strString, strDelim)), strDelim) ; Trim left.
         Break
      Case 2
         strString = ItemExtract (2, StrTrim (StrCat (strDelim, strString)), strDelim) ; Trim right.
         Break
      Case 3
         strString = StrTrim (strString)
         Break
      EndSwitch
      Break
   Case @TRUE
      Switch intBound
      Case 1
      Case 3
         intPos = 1
         While StrSub (strString, intPos, 1) == strChar
            intPos = intPos + 1
         EndWhile
         strString = StrSub (strString, intPos, -1)
         If strString == "" Then Break
         Continue
      Case 2
      Case 3
         intPos = StrLen (strString)
         While StrSub (strString, intPos, 1) == strChar
            intPos = intPos - 1
         EndWhile
         strString = StrSub (strString, 1, intPos)
         Break
      EndSwitch
      Break
   EndSwitch
   If strString == "" Then Break
EndWhile
Return strString
;..........................................................................................................................................
; This UDF "udfStrStrip" returns a substring from the given strString.
; Depending on whether parameter "strBoundary" is "L" ("Left"), "R" ("Right"), or "B" ("Both"),
; the leading, trailing, or both leading and trailing occurrences of "strStripChars"
; will be deleted and the result string will be returned.
;
; Examples:
;   udfStrStrip ('  text  ', ' '   , 'B')   ==> 'text'
;   udfStrStrip ('  text  ', ' '   , 'L')   ==> 'text  '
;   udfStrStrip ('  text  ', ' '   , 'R')   ==> '  text'
;   udfStrStrip ('--text--', '-'   , 'B')   ==> 'text'
;   udfStrStrip ('  text  ', ' t'  , 'B')   ==> 'ex'
;   udfStrStrip ('  text  ', ' t'  , 'L')   ==> 'ext  '
;   udfStrStrip ('  text  ', ' t'  , 'R')   ==> '  tex'
;   udfStrStrip ('  text  ', 'txe' , 'B')   ==> '  text  '
;   udfStrStrip ('  text  ', ' txe', 'R')   ==> ''
;..........................................................................................................................................
; Detlev Dalitz.20030924.20090423.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

; No strip chars, no change.
strString     = "  text  "
strStripChars = ""
strBoundary   = "B"
strResult11   = udfStrStrip (strString, strStripChars, strBoundary) ; "  text  "

; Strip blank chars from the left side.
strString     = "  text  "
strStripChars = " "
strBoundary   = "L"
strResult21   = udfStrStrip (strString, strStripChars, strBoundary) ; "text  "

; Strip blank chars from the right side.
strString     = "  text  "
strStripChars = " "
strBoundary   = "R"
strResult22   = udfStrStrip (strString, strStripChars, strBoundary) ; "  text"

; Strip blank chars from both sides.
strString     = "  text  "
strStripChars = " "
strBoundary   = "B"
strResult23   = udfStrStrip (strString, strStripChars, strBoundary) ; "text"


; Strip blank chars and "t" chars from the left side.
strString     = "  text  "
strStripChars = " t"
strBoundary   = "L"
strResult31   = udfStrStrip (strString, strStripChars, strBoundary) ; "ext  "

; Strip blank chars and "t" chars from the rigth side.
strString     = "  text  "
strStripChars = " t"
strBoundary   = "R"
strResult32   = udfStrStrip (strString, strStripChars, strBoundary) ; "  tex"

; Strip blank chars and "t" chars from both sides.
strString     = "  text  "
strStripChars = " t"
strBoundary   = "B"
strResult33   = udfStrStrip (strString, strStripChars, strBoundary) ; "ex"


; Strip chars " ", "x", "t" from both sides.
strString     = "  text  "
strStripChars = " xt"
strBoundary   = "B"
strResult41   = udfStrStrip (strString, strStripChars, strBoundary) ; "e"

; Strip chars " ", "x", "t", "e" from both sides.
strString     = "  text  "
strStripChars = " xte"
strBoundary   = "B"
strResult42   = udfStrStrip (strString, strStripChars, strBoundary) ; ""

; Strip chars "x", "t", "e", " " from right side.
strString     = "  text  "
strStripChars = "xte "
strBoundary   = "R"
strResult43   = udfStrStrip (strString, strStripChars, strBoundary) ; ""

; Strip chars "x", "t", "e" from both sides.
strString     = "  text  "
strStripChars = "xte"
strBoundary   = "B"
strResult44   = udfStrStrip (strString, strStripChars, strBoundary) ; "  text  "

Exit
;------------------------------------------------------------------------------------------------------------------------------------------