Coldfusion string :: split () problem

I have the following code

<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>

This leads to the following error

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.

Why is this? This happens at compilation, not at startup.

+3
source share
2 answers

CF cannot access the partition results as an array directly from a function call. You need an intermediate variable.

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

You also need to look at your indexes. Despite the fact that the java array below it has an index of 0, using coldfusion to get it makes it indexed to 1.

+2
source

CF 9 added the ability to access partition results as an array directly from a function call. The following works as expected on my local installation of 9.0.1:

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

In this example, the dump shows 'this'.

+3
source

Source: https://habr.com/ru/post/1792604/


All Articles