How do I concatenate two strings and store them into the same struct key
I'm using Coldfusion. I want to concatenat开发者_如何学运维e two strings into the same struct key, but I keep getting an error of "can't convert x into a boolean."
For example:
<cfset myStruct.string1 = nodes[1].string1.XmlText>
<cfset mystruct.string2 = nodes[1].string2.XmlText>
Neither of the following works
<cfset myStruct.concatendatedSring = nodes[1].string1.XmlText AND nodes[1].string2.XmlText>
<cfset myStruct.concatendatedSring = myStruct.string1 AND myStruct.string2>
Why does neither method work?
&
is the string concat operator, AND
and &&
are boolean operators.
<cfset myStruct.concatendatedSring = myStruct.string1 & myStruct.string2>
I've done a number of informal tests on CF10 through 4 different ways to concatenate strings and the results are surprising. I did 50k iterations of appending "HELLO" in various ways. I've includes some rough data below in order from slowest to quickest. These numbers were consistent across 10 different requests, hence the average:
string1 = "#string1##string2#"; // ~4800ms
string1 = string1 & string2; // ~ 4500ms
string1 &= string2; // ~4200ms
string1 = createObject("java", "java.lang.StringBuffer").init();
string1.append(string2); // ~250ms
These fall in the order that I expected, but was surprised at how much quicker the StringBuffer
was. I feel you're going to get the most out of this when concatenating large arounds of String data, such as a CSV or similar. There's no detailed test I performed that weighed one option over the other in typical one-off operations.
In addition to Henry's answer, you can also concatenate two strings like this:
<cfset myStruct.concatendatedSring="#myStruct.string1##myStruct.string2#">
精彩评论