返回 Stringbuilder 内容
我知道很简单,但只是感兴趣
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一种您实际上不需要担心的微观优化。 不过,无论如何,我将发布我认为最优雅(且最有效)的方法:
这可以避免将空 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:
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).
无论你做什么,你都会分配“字符串空间”。 无论是否将值分配给变量,ToString 函数都会为您提供一个字符串。 因此,我建议您最好将 ToString() 的值分配给变量,然后测试该变量值是否为空字符串。 类似于(抱歉,我是 C# 人员,但希望这能在 VB 中工作):
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):
您应该避免在 StringBuilder 上调用 ToString 然后向其追加更多内容。 当您调用 ToString 方法时,您将获得 StringBuilder 内部使用的字符串。 如果您随后向 StringBuilder 追加更多内容,则它必须分配一个新字符串。
只需使用 Length 属性来检查 StringBuilder 是否为空,如果是,则无需使用 StringBuilder 来创建结果。
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.
您可以使用您的 Length 属性StringBuilder 对象。 这样您就可以避免第一次调用 ToString():
或
You could use the Length property of your StringBuilder object. This way you can avoid calling ToString() the first time:
or