Whatever message this page gives is out now! Go check it out!

ListFindLast

Last update:
May 18, 2026
Searches a delimited list and returns the 1-based position of the last occurrence of a value.

Description

Searches from the end and returns the position of the last occurrence.

Function Signature

ListFindLast(String list, String value [, String delimiter [, boolean includeEmptyFields]])

Member Function Syntax

list.listFindLast(String value [, String delimiter [, boolean includeEmptyFields]])
ParameterTypeDescription
listString (required)The delimited list to search.
valueString (required)The value to search for. The comparison is case-sensitive.
delimiterStringDefault: ",". One or more delimiter characters. Each character in the string acts as a separate delimiter.
includeEmptyFieldsbooleanDefault: false. If true, empty elements between consecutive delimiters are counted as list positions. If false, consecutive delimiters are treated as a single delimiter.

Return Value

Integer — Returns the 1-based position of the last occurrence of value in list, or 0 if not found.

Basic — Find Last Occurrence


<cfscript>
simpleList = "red,blue,green,red,yellow,blue";
lastRed = ListFindLast(simpleList, "red");
writeOutput("Last 'red' at position: " & lastRed);
// Output: Last 'red' at position: 4
</cfscript>

Compare ListFind vs ListFindLast


<cfscript>
list1 = "first,second,first,third,first";
firstResult = list1.listFind("first");
lastResult = list1.listFindLast("first");
writeOutput("ListFind: " & firstResult & ", ListFindLast: " & lastResult);
// Output: ListFind: 1, ListFindLast: 5
</cfscript>

Custom Delimiter


<cfscript>
customList = "item1;item2;item1;item3;item2";
lastItem1 = ListFindLast(customList, "item1", ";");
writeOutput("Last 'item1' at position: " & lastItem1);
// Output: Last 'item1' at position: 3
</cfscript>

Multiple Delimiters


<cfscript>
list1 = "one.two,three;one|four";
result = list1.listFindLast("one", ".,;|");
writeOutput("Multiple delimiters: " & result);
// Output: Multiple delimiters: 4
</cfscript>

includeEmptyFields Parameter


<cfscript>
listWithEmpty = "first,,third,,fifth";

// With includeEmptyFields=true — empty elements count as positions
lastEmpty = ListFindLast(listWithEmpty, "", ",", true);
writeOutput("Last empty field (includeEmpty=true): " & lastEmpty);
// Output: Last empty field (includeEmpty=true): 4

// With includeEmptyFields=false — empty elements are ignored
result = ListFindLast(listWithEmpty, "", ",", false);
writeOutput("Last empty field (includeEmpty=false): " & result);
// Output: Last empty field (includeEmpty=false): 0
</cfscript>

Case Sensitivity


<cfscript>
list1 = "Apple,apple,APPLE,Apple";
writeOutput("Last 'Apple': " & list1.listFindLast("Apple"));   // 4
writeOutput("Last 'apple': " & list1.listFindLast("apple"));   // 2
writeOutput("Last 'APPLE': " & list1.listFindLast("APPLE"));   // 3
writeOutput("Last 'aPPle': " & list1.listFindLast("aPPle"));   // 0 (not found)
</cfscript>

Member Function Syntax


<cfscript>
list1 = "apple,banana,apple,cherry,apple";

// 1-arg member function
result1 = list1.listFindLast("apple");
writeOutput("list.listFindLast(value): " & result1);
// Output: 5

// 2-arg member function with delimiter
list2 = "alpha;beta;alpha;gamma";
result2 = list2.listFindLast("alpha", ";");
writeOutput("list.listFindLast(value, delim): " & result2);
// Output: 3
</cfscript>

Named Parameters (Any Order)


<cfscript>
myList = "apple,banana,apple,cherry,apple";

// Standard order
r1 = ListFindLast(list=myList, value="apple");
writeOutput("list=, value=: " & r1);   // 5

// Reversed order
r2 = ListFindLast(value="banana", list=myList);
writeOutput("value=, list=: " & r2);   // 2

// 4-arg named, reordered
listWithEmpty = "p,,q,,p";
r3 = ListFindLast(includeEmptyFields=true, list=listWithEmpty, value="p", delimiter=",");
writeOutput("4-arg reordered: " & r3);   // 5
</cfscript>

Not Found — Returns 0


<cfscript>
list1 = "apple,banana,cherry";
result = list1.listFindLast("orange");
writeOutput("Non-existent 'orange': " & result);
// Output: Non-existent 'orange': 0
</cfscript>

Empty List: Returns 0


<cfscript>
list1 = "";
result = ListFindLast(list1, "anything");
writeOutput("Empty list: " & result);
// Output: Empty list: 0
</cfscript>

Practical Pattern — Replace Last Occurrence


<cfscript>
list1 = "old,new,old,newer,old";
lastOldPos = list1.listFindLast("old");
if (lastOldPos > 0) {
    newList = ListSetAt(list1, lastOldPos, "newest");
}
writeOutput("Original: " & list1);
writeOutput("Modified: " & newList);
// Output:
// Original: old,new,old,newer,old
// Modified: old,new,old,newer,newest
</cfscript>

Large List (1000+ Elements)


<cfscript>
largeList = "";
for (i = 1; i <= 1000; i++) {
    if (i > 1) largeList = largeList & ",";
    largeList = largeList & "item" & i;
}
largeList = largeList & ",target";
result = ListFindLast(largeList, "target");
writeOutput("Target at position: " & result);
// Output: Target at position: 1001
</cfscript>

UDF Wrapping ListFindLast


<cfscript>
function findLastInList(required string list, required string value, string delimiter = ",") {
    return ListFindLast(list, value, delimiter);
}

list1 = "apple,banana,apple,cherry,apple";
result = findLastInList(list1, "apple");
writeOutput("UDF result: " & result);
// Output: UDF result: 5
</cfscript>

Closure Calling ListFindLast


<cfscript>
findLastClosure = function(list, value) {
    return list.listFindLast(value);
};
list1 = "one,two,one,three,one";
result = findLastClosure(list1, "one");
writeOutput("Closure result: " & result);
// Output: Closure result: 5
</cfscript>

Usage Notes

  • The search is case-sensitive.
  • Each character in delimiter is treated as an independent delimiter.
  • The return value is based on list position, not character offset.
  • Use includeEmptyFields=true when empty list items must be counted as valid positions.

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page