How to return a partial row in ColdFusion

Say I have the line The big brown fox jumped<br> over the fence. How can I indicate that I want to save only those characters that are before the <br> tag, deleting the rest?

+4
source share
6 answers

I use something similar, I don’t know if it is more effective.

 <cfset string = "The big brown fox jumped<br> over the fence."> <cfset firstPiece = listGetAt(string, 1, "<br>")> <cfoutput>#firstPiece#</cfoutput> 

I like it because I define output before output, not output. But orange peaks are good because you can adapt your result based on context.

EDIT

As stated in the comments, the above code does produce β€œThe” because of how CF treats each character as a delimiter.

But <cfset firstPiece = listGetAt(string, 1, "<") produces "The big brown fox jumped."

From CF documentation

Delimiters

A string or variable containing one. Character (s) that shares the list of items. The default value is a comma.

If this parameter contains more than one character, ColdFusion processes each occurrence of each character as a separator.

+3
source

If you don't mind giving up the part of the old java that underlies CF ... The AColdFusion string is actually a java string. Java splitting uses a regular expression, which is most easily the only string you want to split. Thus, unlike listToArray (which was expanded in cf9 to allow multi-character splits, by the way), it is by definition multi-character. And since this is a regular expression, if you want it to be case insensitive, this can also be easily done.

So your line:

 <cfset variables.myString = "The big brown fox jumped<br> over the fence." /> <cfset variables.myStringArray = variables.myString.split("(<[bB][Rr]>)",2) /> <cfset variables.myString = variables.myStringArray[1] /> 

variables.myStringArray will contain an array with no more than two elements, part before the first <br>, and part after the first <br> (the second parameter to split, 2, says that it is divided into 2 parts, at most), which will leave any value of <br> in the second part of your line is untouched.

+2
source

Everyone loves regex. So here we use an approach that uses string functions:

 <cfset str = "The big brown fox jumped<br> over the fence."> <!--- roughly the equivalent of str.split("<br>") ---> <cfset pieces = listToArray(str, "<br>")> <cfoutput>#pieces[1]#</cfoutput> 

Update

Meh, you don’t have a CF server. Stupid listToArray() . Here is the one that should work.

 <cfset str = "The big brown fox jumped<br> over the fence."> <cfset pos = find(str, "<br>")> <!--- probably off by one here, don't have CF server handy ---> <cfset piece = left(str, pos)> 

Update # 2

You now have access to the CF server. It works. Refers to all comments:

 <cfset str = "The big brown fox jumped<br> over the fence."> <cfset pos = findNoCase("<br>", str)> <cfset piece = (pos ? left(str, pos - 1) : str)> <cfoutput>#piece#</cfoutput> 
+1
source

Using regular expressions:

 <cfset str = "The big brown fox jumped<br> over the fence." /> <cfset firstPart = REReplaceNoCase( str, "<br>.*$", "") /> <cfoutput>#firstPart#</cfoutput> 
0
source

Why not use the listFirst function, as in:

 <cfset str = "The big brown fox jumped<br> over the fence." /> <cfset firstPart = listfirst(str, "<br>") /> <cfoutput>#firstPart#</cfoutput> 
-2
source

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


All Articles