如何在 cfscript 中将逗号分隔的字符串拆分为数组
有没有一种简单的方法可以使用 cfscript 将逗号分隔的字符串拆分为数组?
类似于以下 JavaScript 的内容:
var a = "a,b,c".split(",");
Is there an easy way to split a comma delimited string into an array using cfscript?
Something similar to the following JavaScript:
var a = "a,b,c".split(",");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
https://cfdocs.org/listtoarray
https://cfdocs.org/listtoarray
您的两个主要选项是 listToArray(myList) 和 java 方法 myList.split(),如前面的答案和评论中所述。但有一些事情需要注意。
例如:
Re java split:
与通过 ColdFusion 层弹出的其他 java 功能一样,这
在 Adobe ColdFusion 8、9 和 10 中没有记录且不受支持,但在 Railo 中则不然,这是一种语法错误:
但这有效:
据我所知,Adobe ColdFusion 将 .split() 的结果视为 ColdFusion 数组:
在 Railo 中不受支持:
这与使用 createObject("java", "java.util.ArrayList") 创建的真正的 java 数组形成鲜明对比。
注意:这只是部分正确;参见下面的编辑。
编辑:谢谢Leigh,我纠正了,我应该坚持我所知道的,CF 比 java 更重要。
我对评论说 .split() 的结果“不是 ColdFusion 数组,而是本机 Java 数组。您将无法通过 CF 修改它”,但根据我的经验,情况并非如此。我试图通过更具体的方式来澄清这一点,这是不明智且不必要的。
Your two main options are listToArray(myList) and the java method myList.split(), as noted in previous answers and comments. There are some things to note though.
For example:
Re java split:
Like other java functionality that pokes up through the ColdFusion layer, this is undocumented and unsupported
In Adobe ColdFusion 8, 9, and 10, but not in Railo, this is a syntax error:
But this works:
As far as I can see, Adobe ColdFusion treats the result of .split() like a ColdFusion array:
In Railo:
That's in contrast to real java arrays, created with createObject("java", "java.util.ArrayList").
NOTE: That's only partly correct; see edit below.
Edit: Thanks Leigh, I stand corrected, I should stick to what I know, which is CF way more than java.
I was reacting to the comment saying that the result of .split() "is not a ColdFusion array, but a native Java array. You won't be able to modify it via CF", which is not the case in my experience. My attempt to clarify that by being more specific was ill-informed and unnecessary.