udfItemListCsvReplaceV4
str udfItemListCsvReplaceV4 (str, int, str, int, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListCsvReplaceV4 (strItem, intIndex, strList, intFlag, strDelimiter)
If strList == "" Then Return strList
intCount = ItemCountCSV (strList, intFlag, strDelimiter)
If !intCount Then Return strList
If intIndex > intCount Then intIndex = intCount ; ... or better immediately return strList ???
If intIndex < 0 Then intIndex = intCount
If !!StrIndex (strItem, '"', 1, @FWDSCAN) Then strItem = StrReplace (strItem, '"', '""')
strListNew = ""
intLast = intIndex - 1
For intI = 1 To intLast
   strListNew = strListNew : strDelimiter : '"' : ItemExtractCSV (intI, strList, intFlag, strDelimiter) : '"'
Next
strListNew = strListNew : strDelimiter : '"' : strItem : '"'
intFirst = intIndex + 1
For intI = intFirst To intCount
   strListNew = strListNew : strDelimiter : '"' : ItemExtractCSV (intI, strList, intFlag, strDelimiter) : '"'
Next
Return StrSub (strListNew, StrLen (strDelimiter) + 1, -1)
;..........................................................................................................................................
; This UDF "udfItemListCsvReplaceV4" replaces an item in a list with a new item.
; It returns a new list, with the specified replacement made; the original list is unchanged.
; For example, specifying an index of 1 causes the first item in the list to be replaced with the new item.
; Using -1 as the index will replace the last item in the list.
;
; Note:
; A null list ("") is not a valid list.
;
; (c)Detlev Dalitz.20120115.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strCsvLineIn = `1,"lastname,firstname",3,4,,"some other words",7`

; Test 1.
strCsvLineOut = udfItemListCsvReplaceV4 (`"test item 3"`, 3, strCsvLineIn, 0, ",") ; Change item at position 3.

strMsgTitle = "udfItemListCsvReplaceV4|Test.1|Replace item 3"
strMsgText = "strCsvLineIn = " : @LF : strCsvLineIn : @LF : @LF : "strCsvLineOut = " : @LF : strCsvLineOut
Pause (strMsgTitle, strMsgText)


; Test 2.
strCsvLineOut = udfItemListCsvReplaceV4 (`"test last item"`, -1, strCsvLineIn, 0, ",") ; Change item at last position.

strMsgTitle = "udfItemListCsvReplaceV4|Test.2|Replace last item"
strMsgText = "strCsvLineIn = " : @LF : strCsvLineIn : @LF : @LF : "strCsvLineOut = " : @LF : strCsvLineOut
Pause (strMsgTitle, strMsgText)


; Test 3.
strCsvLineIn = ""
strCsvLineOut = udfItemListCsvReplaceV4 (`"test last item"`, -1, strCsvLineIn, 0, ",") ; Change item at last position.

strMsgTitle = "udfItemListCsvReplaceV4|Test.3|Null list"
strMsgText = "strCsvLineIn = " : @LF : strCsvLineIn : @LF : @LF : "strCsvLineOut = " : @LF : strCsvLineOut
Pause (strMsgTitle, strMsgText)

:CANCEL
Exit