vb.net 字符串连接字符串 +函数输出 +字符串 = 字符串 +函数输出,仅此而已
以下输出生成一个没有结束 xml 标记的字符串。
m_rFlight.Layout = m_rFlight.Layout + "<G3Grid:Spots>" + Me.gvwSpots.LayoutToString() + "</G3Grid:Spots>"
下面的代码工作正常
m_rFlight.Layout = m_rFlight.Layout + "<G3Grid:Spots>" + Me.gvwSpots.LayoutToString()
m_rFlight.Layout = m_rFlight.Layout + "</G3Grid:Spots>" 'add closing tag
这里发生了什么,第一个示例不起作用而第二个示例不起作用的原因是什么?
gvwSpots.LayoutToString() 函数返回一个字符串。
The following output produces a string with no closing xml tag.
m_rFlight.Layout = m_rFlight.Layout + "<G3Grid:Spots>" + Me.gvwSpots.LayoutToString() + "</G3Grid:Spots>"
This following code works correctly
m_rFlight.Layout = m_rFlight.Layout + "<G3Grid:Spots>" + Me.gvwSpots.LayoutToString()
m_rFlight.Layout = m_rFlight.Layout + "</G3Grid:Spots>" 'add closing tag
What's going on here, what's the reason the first example isnt working and the second is?
The gvwSpots.LayoutToString() function returns a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Meta-Knight 所说,除了我建议使用 StringBuilder 类:
As Meta-Knight said, except that I would recommend using the StringBuilder class:
考虑以下代码,它应该与您的代码等效:
我测试了它,在两种情况下输出都是:SomeValue
如果您没有得到相同的结果,那么这是因为
m_rFlight.Layout
不是字符串,或者Me.gvwSpots.LayoutToString()
不返回字符串并且对 + 做了一些奇怪的事情操作员。您可以使用 &运算符来确保仅执行字符串连接。Consider the following code that should be the equivalent of your code:
I tested it and in both cases the output is:
<G3Grid:Spots>SomeValue</G3Grid:Spots>
If you don't get the same results then it's because either
m_rFlight.Layout
is not a string, orMe.gvwSpots.LayoutToString()
doesn't return a string and does something strange with the + operator. You can use the & operator instead to make sure that only string concatenation is performed.您可以使用 string.concat
或如 Meta-Knight 提到的 &而不是+。 (在连接之前它总是会转换为字符串。)
You can use string.concat
or, as Meta-Knight mentioned, & instead of +. (It will always convert to string before concatenation.)