ColdFusion的字符串::分裂()问题
问题描述:
我有以下代码ColdFusion的字符串::分裂()问题
<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.
这是为什么?这发生在编译,而不是运行。
答
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.
答
CF 9增加了直接从函数调用中作为数组访问分割结果的功能。以下在9.0.1的本地安装上按预期工作:
<cfset foo = "this is a string" />
<cfdump var="#foo.split(" ")[1]#" />
转储在此示例中显示“this”。
+1
你可以发布一个关于split()的文档的链接。我很难找到任何东西。谢谢... – cfEngineers 2011-02-28 17:22:19
很好的答案,但也见我的。 CF9可以处理这个问题。 – 2011-02-17 14:21:56