udfStrInsert
str udfStrInsert (str, str, int, int, str)
;----------------------------------------------------------------------------------------------------------------------
#DefineFunction udfstrInsert (strNew, strTarget, intStart, intLength, strPadChar)
If !StrLen (strNew) Then Return strTarget ; Nothing to do.
intTargetLength = StrLen (strTarget)
If !intTargetLength Then Return ""
If strPadChar == "" Then strPadChar = " "
intStart = Max (0, intStart)
intLength = Max (0, intLength)
Switch @TRUE
Case intStart > intTargetLength
   Return StrCat (strTarget, StrFix (strNew, strPadChar, intLength))
   Break
Case intStart < 1
   Return StrCat (StrFix (strNew, strPadChar, intLength), strTarget)
   Break
Case @TRUE
   Return StrCat (StrSub (strTarget, 1, intStart-1), StrFix (strNew, strPadChar, intLength), StrSub (strTarget, intStart, -1))
   Break
EndSwitch
;..........................................................................................................................................
; Returns the result of inserting string strNew into string strTarget at position intStart.
; Prior to the insertion, strNew is truncated to intLength
; or padded to intLength with the strPadChar character, which defaults to a blank.
;..........................................................................................................................................
; Detlev Dalitz.20020616.20020725
;..........................................................................................................................................
#EndFunction
;----------------------------------------------------------------------------------------------------------------------


; Test.

strMsgTitle = "Demo: udfstrInsert (strNew, strTarget, intStart, intLength, strPadChar)"

:Test1
strT1 = "inserting,the result of string,12,10,"  ; "the result inserting of string"
strT2 = "inserting,the result of string,12,12,*" ; "the result inserting***of string"
strT3 = "inserting,the result of string,0,6,*"   ; "insertthe result of string"
strT4 = "inserting,the result of string,40,6,*"  ; "the result of stringinsert"
strT5 = "inserting,the result of string,-3,6,*"  ; "insertthe result of string"
strT6 = "inserting,the result of string,5,0,"    ; "the result of string"
strT7 = "inserting,the result of string,15,2,"   ; "the result of instring"
strT8 = ",the result of string,15,2,"            ; "the result of string"


For intItem = 1 To 8
   strNew     = ItemExtract (1, strT%intItem%, ",")
   strTarget  = ItemExtract (2, strT%intItem%, ",")
   intStart   = ItemExtract (3, strT%intItem%, ",")
   intLength  = ItemExtract (4, strT%intItem%, ",")
   strPadChar = ItemExtract (5, strT%intItem%, ",")
   strResult  = udfstrInsert (strNew, strTarget, intStart, intLength, strPadChar)

   strMsgText = ""
   strMsgText = StrCat (strMsgText, 'strNew'    , @TAB, @TAB, '"', strNew    , '"', @LF)
   strMsgText = StrCat (strMsgText, 'strTarget' , @TAB, @TAB, '"', strTarget , '"', @LF)
   strMsgText = StrCat (strMsgText, 'intStart'  , @TAB, @TAB, '"', intStart  , '"', @LF)
   strMsgText = StrCat (strMsgText, 'intLength' , @TAB, @TAB, '"', intLength , '"', @LF)
   strMsgText = StrCat (strMsgText, 'strPadChar', @TAB, @TAB, '"', strPadChar, '"', @LF)
   strMsgText = StrCat (strMsgText, 'strResult' , @TAB, @TAB, '"', strResult , '"', @LF)
   Message (strMsgTitle, strMsgText)
Next


:Test2
BoxOpen ("Demo: udfstrInsert (strNew, strTarget, intStart, intLength, strPadChar)", "")
intTargetLength = 10
strTarget = StrFill (" ", intTargetLength)
For intLen = 1 To intTargetLength
   strOut = udfstrInsert ("*", strTarget, intLen, 1, "")
   strOut = StrCat ("[", StrSub (strOut, 1, intTargetLength), "]")
   BoxText (strOut)
   TimeDelay (0.2 / intTargetLength)
Next
For intLen = intTargetLength To 1 By -1
   strOut = udfstrInsert ("*", strTarget, intLen, 1, "")
   strOut = StrCat ("[", StrSub (strOut, 1, intTargetLength), "]")
   BoxText (strOut)
   TimeDelay (0.2 / intTargetLength)
Next
BoxShut ()


; Note: The user defined function udfStrInsert has been made its task
; by the newly introduced native WinBatch function StrInsert,
; first showing up in WB 2007B (DLL 5.13bem).
; StrInsert (base-string, new-string, [pad-string [, start [, length]]])

:Test3
BoxOpen ("Demo: StrInsert (strTarget, strNew, strPadChar, intStart, intLength)","")
intTargetLength = 10
strTarget = StrFill (" ", intTargetLength)
For intLen = 1 To intTargetLength
   strOut = StrInsert (strTarget, "*", "", intLen, 1)
   strOut = StrCat ("[", StrSub (strOut, 1, intTargetLength), "]")
   BoxText (strOut)
   TimeDelay (0.2 / intTargetLength)
Next
For intLen = intTargetLength To 1 By -1
   strOut = StrInsert (strTarget, "*", "", intLen, 1)
   strOut = StrCat ("[", StrSub (strOut, 1, intTargetLength), "]")
   BoxText (strOut)
   TimeDelay (0.2 / intTargetLength)
Next
BoxShut ()

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