2014-01-13 41 views
1

過濾列表,我有ID(1,2,3,4,5,6,7,8,9,10)從列表ColdFusion的

的主列表,我有ID的列表我想從主列表中刪除(2,5,8)

在coldfusion的主列表中過濾這些ID最簡單的方法是什麼?

謝謝!

+0

您可以使用替換功能http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_m-r_32.html –

+2

沒有,替換搜索子字符串,而不是整個列表元素。也許你的意思是[ReplaceList](http://livedocs.adobe.com/coldfusion/8/functions_m-r_33.html)?此外,cflib.org也是檢查這類任務的好地方http://cflib.org/udf/listRemoveList – Leigh

+0

我不好你是對的 –

回答

6

ReplaceList:

<cfset l1 = "1,2,3,4,5,6,7,8"> 
<cfset L2 = "2,5,8"> 

<cfoutput>#ReplaceList(L1,L2,'')#</cfoutput> 

這將讓你有額外的逗號,其名單的ColdFusion都OK使用。如果你想刪除它們,然後通過數組功能運行它們:

<cfset l1 = "1,2,,3,4,5,6,7,8"> 
<cfset L2 = "2,5,8"> 
<cfset L3 = ArrayToList(ListToArray(ReplaceList(L1,L2,'')))> 
<cfoutput>#L3#</cfoutput> 
+0

真棒,我沒有知道這個功能。謝謝! – RandyLahey

2

一個優雅的解決方案是可行的CF 10+和Railo 4+通過Underscore.cfc's without function

_ = new Underscore();// instantiate the library 

// prepare the original list 
origList = "1,2,3,4,5,6,7,8,9,10"; 
origArray = listToArray(origList); 

// get a new array of values without 2, 5, or 8 
newArray = _.without(origArray, [2, 5, 8]);// returns [1,3,4,6,7,9,10] 

然後可以使用arrayToList()來如有必要,轉換回列表。

注:我寫Underscore.cfc