string 如何连接两个字符串并将它们存储到相同的结构键中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3622667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I concatenate two strings and store them into the same struct key
提问by Mohamad
I'm using Coldfusion. I want to concatenate two strings into the same struct key, but I keep getting an error of "can't convert x into a boolean."
我正在使用 Coldfusion。我想将两个字符串连接到同一个结构键中,但我不断收到“无法将 x 转换为布尔值”的错误消息。
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?
为什么这两种方法都不起作用?
回答by Henry
&
is the string concat operator, AND
and &&
are boolean operators.
&
是字符串连接运算符,AND
并且&&
是布尔运算符。
<cfset myStruct.concatendatedSring = myStruct.string1 & myStruct.string2>
回答by Tristan Lee
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:
我通过 4 种不同的连接字符串的方式对 CF10 进行了许多非正式测试,结果令人惊讶。我以各种方式重复了 50k 次附加“HELLO”。我在下面按从最慢到最快的顺序列出了一些粗略的数据。这些数字在 10 个不同的请求中是一致的,因此平均值为:
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.
这些按我预期的顺序下降,但对速度之快感到惊讶StringBuffer
。我觉得在连接大量字符串数据(例如 CSV 或类似数据)时,您会从中获得最大收益。在典型的一次性操作中,我没有进行任何详细的测试来权衡一个选项。
回答by Gert Grenander
In addition to Henry's answer, you can also concatenate two strings like this:
除了亨利的回答,你还可以像这样连接两个字符串:
<cfset myStruct.concatendatedSring="#myStruct.string1##myStruct.string2#">