ListValueCount 的正确使用方法

Correct way to use ListValueCount

我需要计算给定字符串中 ? 的总数。这是我的代码:

<cfscript>
    myString="UPDATE personnel SET personnel_id = ?, personnel_name = ?, personnel_nric = ?, personnel_staff_num = ?, personnel_designation = ?, personnel_department = ?, personnel_appointed_start_date = ?, personnel_appointed_end_date = ?, personnel_inspection_date = ?, personnel_org_id = ?, lt_dispose_personnel_type_id = ? WHERE id = 49;"
    writeOutput("The count of ? is: " & ListValueCount(myString,"?",","))
</cfscript>

但这是我的输出:The count of ? is: 0。我的预期输出是 11ListValueCount 的正确使用方法是什么?提前致谢。

CFfiddle:https://cffiddle.org/app/file?filepath=0dee010d-6bde-4ac9-939b-9867b53f128c/da1fade3-c26b-4703-a0b8-0037eddf6345/ecea7294-fba5-464e-ab2f-b919dd94f650.cfm

问题是 ListValueCount() 对列表中指定值的实例进行计数(请参阅 listValueCount at cfdocs)。

因为您的分隔符是 ",",列表将分解为以下列表项值(请参阅此处的修剪值):

1. "UPDATE personnel SET personnel_id = ?"
2. "personnel_name = ?"
3. "personnel_nric = ?"
4. "personnel_staff_num = ?"
5. "personnel_designation = ?"
6. "personnel_department = ?"
7. "personnel_appointed_start_date = ?"
8. "personnel_appointed_end_date = ?"
9. "personnel_inspection_date = ?"
10. "personnel_org_id = ?"
11. "lt_dispose_personnel_type_id = ? WHERE id = 49;"

因为您正在寻找特定的项目值"?",所以找不到项目:上面的每个字符串值都由一个以上的问号组成!

解决方案一:使用正则表达式搜索,例如

<cfscript>
    myString="UPDATE personnel SET personnel_id = ? , personnel_name = ?  , personnel_nric = ?, personnel_staff_num = ?, personnel_designation = ?, personnel_department = ?, personnel_appointed_start_date = ?, personnel_appointed_end_date = ?, personnel_inspection_date = ?, personnel_org_id = ?, lt_dispose_personnel_type_id = ? WHERE id = 49;"
    writeOutput("The count of ? is: " & reFind("\?.+?,?", myString,1,true,"ALL").len())
</cfscript>

解决方案 2: 如果您使用的是更现代的 cfml 引擎,另一种可能性是使用 listReduce() 成员函数循环您的列表,如下所示:

<cfscript>
myString="UPDATE personnel SET personnel_id = ?, personnel_name = ?, personnel_nric = ?, personnel_staff_num = ?, personnel_designation = ?, personnel_department = ?, personnel_appointed_start_date = ?, personnel_appointed_end_date = ?, personnel_inspection_date = ?, personnel_org_id = ?, lt_dispose_personnel_type_id = ? WHERE id = 49;"
writeOutput("The count of ? is: " & myString.listReduce( 
    ( acc, element ) => {
        if( findNoCase( "?", element ) ){
            return acc+1;
        }else{
            return acc;
        }

    }

    , 0, ","
    ));</cfscript>

解决方案 3 将是使用 cfloop 的经典方法并遍历每个项目并执行类似于上面的 listReduce 函数的 findNoCase。

旁注: 我想您正在做某种动态 SQL 查询或调试。请小心创建这些,尤其是在创建动态 SQL 查询时。根据您正在创建的列表的内容,正则表达式解决方案 1 可能会产生副作用。您需要仔细测试一下!

为什么 ListValueCount 不是您要实现的工具的正确工具,@andreasRu 已经很好地解释了。

@andreasRu 提供的答案还有一个替代方案(在评论中),如下所示。我觉得这是最简单的解决方案。

myString="UPDATE personnel SET personnel_id = ?, personnel_name = ?, personnel_nric = ?, personnel_staff_num = ?, personnel_designation = ?, personnel_department = ?, personnel_appointed_start_date = ?, personnel_appointed_end_date = ?, personnel_inspection_date = ?, personnel_org_id = ?, lt_dispose_personnel_type_id = ? WHERE id = 49;"
writeOutput("The count of ? is: " & Len(myString) - Len(Replace(myString, '?', '', 'all')))

我们只是检查初始字符串和所有 ? 被剥离后的字符串之间的长度差异。