Coldfusion string :: split()问题

时间:2011-02-16 18:30:33

标签: string coldfusion split

我有以下代码

<cffunction name="getObjTag" returnType="string" output="false">
    <cfargument name="obj" Type="string" required="true">
    <cfargument name="tagname" Type="string" required="true">
    <cfreturn obj.split("<" & tagname.toUpperCase() & ">")[2]>
</cffunction>

导致以下错误

Invalid CFML construct found on line 96 at column 63.

ColdFusion was looking at the following text:

[

The CFML compiler was processing:

A cfreturn tag beginning on line 96, column 10.
A cfreturn tag beginning on line 96, column 10.

这是为什么?这发生在编译时,而不是运行。

2 个答案:

答案 0 :(得分:3)

CF 9增加了直接从函数调用中作为数组访问拆分结果的功能。以下在我本地安装的9.0.1上按预期工作:

<cfset foo = "this is a string" />
<cfdump var="#foo.split(" ")[1]#" />

转储在此示例中显示“this”。

答案 1 :(得分:2)

CF无法直接从函数调用中访问拆分结果作为数组。你需要一个中间变量。

<cfset var tmpArray = arrayNew(1)/>
<cfset tmpArray = arguments.obj.split("<" & arguments.tagname.toUpperCase() & ">")/>
<cfif arrayLen(tmpArray) gt 1>
   <cfreturn tmpArray[2]/>
<cfelse>
   <cfreturn ""/>
</cfif>

您还需要观察索引。虽然下面的java数组是0索引,但使用coldfusion来获取它使得它被索引为1。