udfStrStrip (2)
str udfStrStrip (str, str, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrStrip (strString, strStripChars, intBoundary)
If strString == "" Then Return strString
If strStripChars == "" Then Return strString

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

intBoundary = Min (Max (intBoundary, -1), 1) ; -1 = Left, 0 = Left+Right, 1 = Right.

While @TRUE
   strBoundaryChars = ""
   Switch intBoundary
   Case -1
   Case 0
      strBoundaryChars = StrSub (strString, 1, 1)
      Continue
   Case 1
   Case 0
      strBoundaryChars = 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 intBoundary
      Case 0
         strString = StrTrim (strString) ; Trim left and right.
         Break
      Case 1
         strString = ItemExtract (2, StrTrim (strDelim : strString), strDelim) ; Trim right.
         Break
      Case -1
         strString = ItemExtract (1, StrTrim (strString : strDelim), strDelim) ; Trim left.
         Break
      EndSwitch
      Break
   Case @TRUE
      Switch intBoundary
      Case -1
      Case 0
         intPos = 1
         While StrIndex (strString, strChar, intPos, @FWDSCAN) == intPos
            intPos = intPos + 1
         EndWhile
         strString = StrSub (strString, intPos, -1)
         If strString == "" Then Break
         Continue
      Case 1
      Case 0
         intPos = StrLen (strString)
         While StrIndex (strString, strChar, intPos, @FWDSCAN) == intPos
            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 "intBoundary" is -1 (left edge), 1 (right edge) or 0 (both edges),
; the leading, trailing, or both leading and trailing occurrences of "strStripChars"
; will be deleted and the result string will be returned.
;
; Examples:
;   udfStrStrip ('  text  ', ' '   , 0)   ==> 'text'
;   udfStrStrip ('  text  ', ' '   , -1)  ==> 'text  '
;   udfStrStrip ('  text  ', ' '   , 1)   ==> '  text'
;   udfStrStrip ('--text--', '-'   , 0)   ==> 'text'
;   udfStrStrip ('  text  ', ' t'  , 0)   ==> 'ex'
;   udfStrStrip ('  text  ', ' t'  , -1)  ==> 'ext  '
;   udfStrStrip ('  text  ', ' t'  , 1)   ==> '  tex'
;   udfStrStrip ('  text  ', 'txe' , 0)   ==> '  text  '
;   udfStrStrip ('  text  ', ' txe', 1)   ==> ''
;..........................................................................................................................................
; Detlev Dalitz.20030924.20090423.20100108.20101219.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

; No strip chars, no change.
strString = "  text  "
strStripChars = ""
intBoundary = 0
strResult11 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">  text  <"

; Strip blank chars from the left side.
strString = "  text  "
strStripChars = " "
intBoundary = -1
strResult21 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">text  <"

; Strip blank chars from the right side.
strString = "  text  "
strStripChars = " "
intBoundary = 1
strResult22 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">  text<"

; Strip blank chars from both sides.
strString = "  text  "
strStripChars = " "
intBoundary = 0
strResult23 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">text<"


; Strip blank chars and "t" chars from the left side.
strString = "  text  "
strStripChars = " t"
intBoundary = -1
strResult31 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">ext  <"

; Strip blank chars and "t" chars from the rigth side.
strString = "  text  "
strStripChars = " t"
intBoundary = 1
strResult32 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">  tex<"

; Strip blank chars and "t" chars from both sides.
strString = "  text  "
strStripChars = " t"
intBoundary = 0
strResult33 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">ex<"

; Strip blank chars and @CR and @LF chars from both sides.
strString = "   " : @CRLF : @CRLF : " text  " : @CR : @CR : " " : @LF
strStripChars = " " : @CRLF
intBoundary = 0
strResult34 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">text<"


; Strip chars " ", "x", "t" from both sides.
strString = "  t ex t xt  "
strStripChars = " xt"
intBoundary = 0
strResult41 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">e<"

; Strip chars " ", "x", "t", "e" from both sides.
strString = "  text  "
strStripChars = " xte"
intBoundary = 0
strResult42 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; "><"

; Strip chars "x", "t", "e", " " from right side.
strString = "  text  "
strStripChars = "xte "
intBoundary = 1
strResult43 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; "><"

; Strip chars "x", "t", "e" from both sides.
strString = "  text  "
strStripChars = "xte"
intBoundary = 0
strResult44 = ">" : udfStrStrip (strString, strStripChars, intBoundary) : "<" ; ">  text  <"

strResult = strResult11
strResult = strResult : @CRLF : strResult21 : @CRLF : strResult22 : @CRLF : strResult23
strResult = strResult : @CRLF : strResult31 : @CRLF : strResult32 : @CRLF : strResult33 : @CRLF : strResult34
strResult = strResult : @CRLF : strResult41 : @CRLF : strResult42 : @CRLF : strResult43 : @CRLF : strResult44
strResult = strResult : @CRLF
ClipPut (strResult)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;   >  text  <
;   >text  <
;   >  text<
;   >text<
;   >ext  <
;   >  tex<
;   >ex<
;   >text<
;   >e<
;   ><
;   ><
;   >  text  <
;------------------------------------------------------------------------------------------------------------------------------------------