What is the best way to convert an array of values in ColdFusion
在ColdFusion中转换值数组的最佳方法是什么
[ Fed Jones, John Smith, George King, Wilma Abby]
and to a list where the last comma is an or
以及最后一个逗号是或的列表
Fed Jones, John Smith, George King or Wilma Abby
I thought REReplace might work but haven't found the right expression yet.
我认为REReplace可能有效但尚未找到正确的表达方式。
3 个解决方案
#1
13
If you've got an array, combining the last element with an ArrayToList is the simplest way (as per Henry's answer).
如果你有一个数组,将最后一个元素与ArrayToList组合是最简单的方法(根据亨利的答案)。
If you've got it as a string, using rereplace is a valid method, and would work like so:
如果你把它作为一个字符串,使用rereplace是一个有效的方法,并将这样工作:
<cfset Names = rereplace( Names , ',(?=[^,]+$)' , ' or ' ) />
Which says match a comma, then check (without matching) that there are no more commas until the end of the string (which of course will only apply for the last comma, and it will thus be replaced).
其中说匹配逗号,然后检查(不匹配)在字符串结尾之前没有逗号(当然只会应用最后一个逗号,因此将被替换)。
#2
5
It'd be easier to manipulate in the array level first, before converting into a list.
在转换为列表之前,首先在数组级别操作会更容易。
names = ["Fed Jones", "John Smith", "George King", "Wilma Abby"];
lastIndex = arrayLen(names);
last = names[lastIndex];
arrayDeleteAt(names, lastIndex);
result = arrayToList(names, ", ") & " or " & last;
// result == "Fed Jones, John Smith, George King or Wilma Abby"
#3
2
Another option is to work with a list / string using listLast and the JAVA lastIndexOf() method of the result string.
另一种选择是使用listLast和结果字符串的JAVA lastIndexOf()方法处理列表/字符串。
<cfscript>
names = ["Fed Jones", "John Smith", "George King", "Wilma Abby"];
result = arraytoList(names,', ');
last = listLast(result);
result = listLen(result) gt 1 ? mid(result, 1, result.lastIndexOf(',')) & ' or' & last : result;
</cfscript>
<cfoutput>#result#</cfoutput>
Result:
Fed Jones, John Smith, George King or Wilma Abby
Fed Jones,John Smith,George King或Wilma Abby
#1
13
If you've got an array, combining the last element with an ArrayToList is the simplest way (as per Henry's answer).
如果你有一个数组,将最后一个元素与ArrayToList组合是最简单的方法(根据亨利的答案)。
If you've got it as a string, using rereplace is a valid method, and would work like so:
如果你把它作为一个字符串,使用rereplace是一个有效的方法,并将这样工作:
<cfset Names = rereplace( Names , ',(?=[^,]+$)' , ' or ' ) />
Which says match a comma, then check (without matching) that there are no more commas until the end of the string (which of course will only apply for the last comma, and it will thus be replaced).
其中说匹配逗号,然后检查(不匹配)在字符串结尾之前没有逗号(当然只会应用最后一个逗号,因此将被替换)。
#2
5
It'd be easier to manipulate in the array level first, before converting into a list.
在转换为列表之前,首先在数组级别操作会更容易。
names = ["Fed Jones", "John Smith", "George King", "Wilma Abby"];
lastIndex = arrayLen(names);
last = names[lastIndex];
arrayDeleteAt(names, lastIndex);
result = arrayToList(names, ", ") & " or " & last;
// result == "Fed Jones, John Smith, George King or Wilma Abby"
#3
2
Another option is to work with a list / string using listLast and the JAVA lastIndexOf() method of the result string.
另一种选择是使用listLast和结果字符串的JAVA lastIndexOf()方法处理列表/字符串。
<cfscript>
names = ["Fed Jones", "John Smith", "George King", "Wilma Abby"];
result = arraytoList(names,', ');
last = listLast(result);
result = listLen(result) gt 1 ? mid(result, 1, result.lastIndexOf(',')) & ' or' & last : result;
</cfscript>
<cfoutput>#result#</cfoutput>
Result:
Fed Jones, John Smith, George King or Wilma Abby
Fed Jones,John Smith,George King或Wilma Abby