返回 Stringbuilder 内容

发布于 2024-07-14 04:04:55 字数 504 浏览 4 评论 0原文

我知道很简单,但只是感兴趣

我有一个 stringbuilder 变量,我想返回其内容,但如果它为空我想返回“|”,所以最好在比较语句中使用 stringbuilder.tostring 例如

   If lReturnStringBuilder.ToString = String.Empty Then
                lReturnStringBuilder.Append("|")
            End If
return lreturnStringBuilder.tostring

或者最好将其转换为字符串并进行比较,即使这意味着加载一个新变量并为其分配字符串空间,例如

Dim lString as string = lReturnStringBuilder.ToString
if lString = string.empty then
      lstring = "|"
end if
return lString

Trivial I know, but just interested

I've got a stringbuilder variable, that I want to return the contents of, but if it's empty I want to return "|", so is it best to use stringbuilder.tostring in the compare statement e.g

   If lReturnStringBuilder.ToString = String.Empty Then
                lReturnStringBuilder.Append("|")
            End If
return lreturnStringBuilder.tostring

or is it best to convert it to a string, and compare that, even though that means loading up a new variable and the allocating string space for that e.g

Dim lString as string = lReturnStringBuilder.ToString
if lString = string.empty then
      lstring = "|"
end if
return lString

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

傲娇萝莉攻 2024-07-21 04:04:55

这是一种您实际上不需要担心的微观优化。 不过,无论如何,我将发布我认为最优雅(且最有效)的方法:

Dim result = If(lReturnString.Length = 0, "|", lReturnString.ToString())

这可以避免将空 StringBuilder 不必要地转换为字符串(或者然后调用 Append,这绝对不是必需的)。 请注意内联 If 语句 (VB 9.0) 的使用,它在任何一种情况下都不会计算这两个语句,因为它是一种语言构造而不是函数(完全等同于带有变量赋值)。

This is the sort of micro-optimisation that you really just don't need to worry about. However, I will post what I would think is most elegant (and efficient) way of doing this anyway:

Dim result = If(lReturnString.Length = 0, "|", lReturnString.ToString())

This saves converting an empty StringBuilder to a string unnecessarily (or then calling Append, which is definitely not required). Note the use of the inline If statement (VB 9.0), which does not evaluate both statements in any one case, as it is a language construct and not a function (exactly equivalent to a normal If statement with variable assignments).

童话 2024-07-21 04:04:55

无论你做什么,你都会分配“字符串空间”。 无论是否将值分配给变量,ToString 函数都会为您提供一个字符串。 因此,我建议您最好将 ToString() 的值分配给变量,然后测试该变量值是否为空字符串。 类似于(抱歉,我是 C# 人员,但希望这能在 VB 中工作):

Dim returnVal as String
returnVal = lReturnString.ToString()
If String.IsNullOrEmpty(returnVal) Then
  returnVal = "|"
End If

You are allocating the "string space" regardless of what you do. The ToString function gives you a string, whether you assign the value to a variable or not. So I would suggest that you would be best off assigning the value of ToString() to a variable and then testing that variable value for an empty string. Something like (sorry, I'm a C# guy, but hopefully this will work in VB):

Dim returnVal as String
returnVal = lReturnString.ToString()
If String.IsNullOrEmpty(returnVal) Then
  returnVal = "|"
End If
画骨成沙 2024-07-21 04:04:55

您应该避免在 StringBuilder 上调用 ToString 然后向其追加更多内容。 当您调用 ToString 方法时,您将获得 StringBuilder 内部使用的字符串。 如果您随后向 StringBuilder 追加更多内容,则它必须分配一个新字符串。

只需使用 Length 属性来检查 StringBuilder 是否为空,如果是,则无需使用 StringBuilder 来创建结果。

If lReturnStringBuilder.Length = 0 Then
   Return "|"
Else
   Return lReturnStringBuilder.ToString()
End If

You should avoid calling ToString on the StringBuilder and then append more to it. When you call the ToString method, you get the string that was used internally by the StringBuilder. If you then append more to the StringBuilder, it has to allocate a new string.

Just use the Length property to check if the StringBuilder is empty, and if it is you don't have to involve the StringBuilder to create the result.

If lReturnStringBuilder.Length = 0 Then
   Return "|"
Else
   Return lReturnStringBuilder.ToString()
End If
昔梦 2024-07-21 04:04:55

您可以使用您的 Length 属性StringBuilder 对象。 这样您就可以避免第一次调用 ToString():

If lReturnStringBuilder.Length = 0 Then
   lReturnStringBuilder.Append("|")
End If

Return lReturnStringBuilder.ToString()

If lReturnStringBuilder.Length = 0 Then
   Return "|"
End If

Return lReturnStringBuilder.ToString()

You could use the Length property of your StringBuilder object. This way you can avoid calling ToString() the first time:

If lReturnStringBuilder.Length = 0 Then
   lReturnStringBuilder.Append("|")
End If

Return lReturnStringBuilder.ToString()

or

If lReturnStringBuilder.Length = 0 Then
   Return "|"
End If

Return lReturnStringBuilder.ToString()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文