如何在 cfscript 中将逗号分隔的字符串拆分为数组

发布于 2024-12-03 08:34:54 字数 132 浏览 0 评论 0原文

有没有一种简单的方法可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

メ斷腸人バ 2024-12-10 08:34:54
var a = ListToArray("a,b,c,d,e,f");     

https://cfdocs.org/listtoarray

var a = ListToArray("a,b,c,d,e,f");     

https://cfdocs.org/listtoarray

守护在此方 2024-12-10 08:34:54

您的两个主要选项是 listToArray(myList) 和 java 方法 myList.split(),如前面的答案和评论中所述。但有一些事情需要注意。

  • 默认情况下,ColdFusion 列表函数会忽略空列表项。
  • 从 ColdFusion 版本 8 开始,listToArray 采用可选的第三个参数 includeEmptyFields,它是一个控制该行为的布尔值,默认为 false。

例如:

listToArray("asdf,,,qwer,tyui") is ["asdf", "qwer", "tyui"]
listToArray("asdf,,,qwer,tyui", ",", true) is ["asdf", "", "", "qwer", "tyui"]

Re java split:

与通过 ColdFusion 层弹出的其他 java 功能一样,这

在 Adob​​e ColdFusion 8、9 和 10 中没有记录且不受支持,但在 Railo 中则不然,这是一种语法错误:

a = "asdf,,,qwer,tyui".split(",")

但这有效:

s = "asdf,,,qwer,tyui";
a = s.split(",");

据我所知,Adobe ColdFusion 将 .split() 的结果视为 ColdFusion 数组:

  • CFDumps 将其显示为数组
  • 它是从 1 开始的
  • 您可以使用 arrayLen
  • 您可以在 ColdFusion 中修改其元素
  • 可能还有其他行为我没有检查,它们与 CF 数组不同,但如上所述,它

在 Railo 中不受支持:

  • 调试转储将其显示为本机数组 (java.lang .String[])
  • 关于其非常类似数组的行为的其他陈述仍然正确,

这与使用 createObject("java", "java.util.ArrayList") 创建的真正的 java 数组形成鲜明对比。
注意:这只是部分正确;参见下面的编辑。

  • 例如,在 Adob​​e ColdFusion 中,无法使用 CFML 直接修改 java ArrayList 的元素。
  • 一般来说,Railo 处理 Java 数组的方式比 ACF 更像 ColdFusion 的

编辑:谢谢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.

  • By default ColdFusion list functions ignore empty list items.
  • As of ColdFusion version 8, listToArray takes an optional third argument, includeEmptyFields, which is a boolean controlling that behavior, defaulting to false.

For example:

listToArray("asdf,,,qwer,tyui") is ["asdf", "qwer", "tyui"]
listToArray("asdf,,,qwer,tyui", ",", true) is ["asdf", "", "", "qwer", "tyui"]

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:

a = "asdf,,,qwer,tyui".split(",")

But this works:

s = "asdf,,,qwer,tyui";
a = s.split(",");

As far as I can see, Adobe ColdFusion treats the result of .split() like a ColdFusion array:

  • CFDumps show it as an array
  • It's 1-based
  • You can use arrayLen on it
  • You can modify its elements in ColdFusion
  • There may be other behaviors I didn't check that aren't like a CF array, but as above, it's unsupported

In Railo:

  • Debug dumps show it as Native Array (java.lang.String[])
  • The other statements about its very array-like behavior are still true

That's in contrast to real java arrays, created with createObject("java", "java.util.ArrayList").
NOTE: That's only partly correct; see edit below.

  • For instance, in Adobe ColdFusion, elements of a java ArrayList can't be modified directly with CFML
  • In general, Railo handles java arrays more like ColdFusion ones than ACF

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文