vb.net 字符串连接字符串 +函数输出 +字符串 = 字符串 +函数输出,仅此而已

发布于 2024-08-20 09:22:41 字数 486 浏览 3 评论 0原文

以下输出生成一个没有结束 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静 2024-08-27 09:22:41

正如 Meta-Knight 所说,除了我建议使用 StringBuilder 类:

Dim myString As New System.Text.StringBuilder

myString.Append("<G3Grid:Spots>")
myString.Append(Me.gvwSpots.LayoutToString())
myString.Append("</G3Grid:Spots>")

m_rFlight.Layout = myString.ToString()

As Meta-Knight said, except that I would recommend using the StringBuilder class:

Dim myString As New System.Text.StringBuilder

myString.Append("<G3Grid:Spots>")
myString.Append(Me.gvwSpots.LayoutToString())
myString.Append("</G3Grid:Spots>")

m_rFlight.Layout = myString.ToString()
枕头说它不想醒 2024-08-27 09:22:41

考虑以下代码,它应该与您的代码等效:

Dim someString As String = String.Empty

someString = someString + "<G3Grid:Spots>" + "SomeValue" + "</G3Grid:Spots>"

Console.WriteLine(someString)

someString = String.Empty
someString = someString + "<G3Grid:Spots>" + "SomeValue"
someString = someString + "</G3Grid:Spots>"

Console.WriteLine(someString)

我测试了它,在两种情况下输出都是: SomeValue

如果您没有得到相同的结果,那么这是因为 m_rFlight.Layout 不是字符串,或者 Me.gvwSpots.LayoutToString() 不返回字符串并且对 + 做了一些奇怪的事情操作员。您可以使用 &运算符来确保仅执行字符串连接。

Consider the following code that should be the equivalent of your code:

Dim someString As String = String.Empty

someString = someString + "<G3Grid:Spots>" + "SomeValue" + "</G3Grid:Spots>"

Console.WriteLine(someString)

someString = String.Empty
someString = someString + "<G3Grid:Spots>" + "SomeValue"
someString = someString + "</G3Grid:Spots>"

Console.WriteLine(someString)

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, or Me.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.

じее 2024-08-27 09:22:41

您可以使用 string.concat

m_rFlight.Layout = string.concat(m_rFlight.Layout, "<G3Grid:Spots>",_
  Me.gvwSpots.LayoutToString(), "</G3Grid:Spots>")

或如 Meta-Knight 提到的 &而不是+。 (在连接之前它总是会转换为字符串。)

m_rFlight.Layout &= "<G3Grid:Spots>" & Me.gvwSpots.LayoutToString() & "</G3Grid:Spots>"

You can use string.concat

m_rFlight.Layout = string.concat(m_rFlight.Layout, "<G3Grid:Spots>",_
  Me.gvwSpots.LayoutToString(), "</G3Grid:Spots>")

or, as Meta-Knight mentioned, & instead of +. (It will always convert to string before concatenation.)

m_rFlight.Layout &= "<G3Grid:Spots>" & Me.gvwSpots.LayoutToString() & "</G3Grid:Spots>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文