StringBuilder相关问题
我已经为堆栈编写了一个程序。 (https://stackoverflow.com/questions/2617367?tab=votes#tab-top ) 为此,我需要一个 StringBuilder 来向我显示堆栈中的内容,否则我将获得类名称而不是内部的实际值。
我的问题是除了 StringBuilder 之外还有其他方法可以解决此类问题吗?
另外还有哪些情况会出现这种问题呢? 另外,当我在一行上需要几个东西时,我编写 StringBuilder 的方式感觉非常尴尬。
public override string ToString()
{
StringBuilder builder = new StringBuilder();
foreach (int value in tabel)
{
builder.Append(value);
builder.Append(" ");
}
if (tabel.Length == tabel.Length) // this is a bit messy, since I couldn't append after the rest above
{
builder.Append("(top:");
builder.Append(top);
builder.Append(")");
}
return builder.ToString();
}/*ToString*/
I have written a program for a stack. (https://stackoverflow.com/questions/2617367?tab=votes#tab-top)
For this i needed a StringBuilder to be able to show me what was in the stack else i would get the class name instead of the actual values inside.
My question is there any other way except for a StringBuilder for such kind of problem?
Also in what other kind of cases does this kind of problem happen?
Also the way i have written the StringBuilder felt very awkward when i needed several things on 1 line.
public override string ToString()
{
StringBuilder builder = new StringBuilder();
foreach (int value in tabel)
{
builder.Append(value);
builder.Append(" ");
}
if (tabel.Length == tabel.Length) // this is a bit messy, since I couldn't append after the rest above
{
builder.Append("(top:");
builder.Append(top);
builder.Append(")");
}
return builder.ToString();
}/*ToString*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Array.ConvertAll 和 String.Join 而不是自己迭代列表。
另外,当你在一行中谈论多个事情时......你在任何地方都没有任何换行符。
或者,如果您继续使用 StringBuilder,Append 方法将返回 StringBuilder,以便您可以将调用链接在一起:
You could use Array.ConvertAll and String.Join instead of iterating the list yourself.
Also, when you talk about multiple things on one line... you don't have any linebreaks anywhere.
Or, if you keep using StringBuilder, the Append method returns the StringBuilder so you can chain calls together:
您可以使用这样的扩展方法来汇总可枚举集合
用法
PS 如果您使用 .NET 版本 4 之前的版本,您可能需要一个 .ToArray() 来满足 string.Join()
或者,更好的是,使用重载:
string Join(string seperator, IEnumerablevalues);
您可以简化为:-用法
You could use an extension method like this to summarize enumerable collections
Usage
PS If you are using .NET prior to version 4 you might need a .ToArray() in there to satisfy string.Join()
Or, better yet, using the overload:
string Join<T>(string separator, IEnumerable<T> values);
you can simplify to:-Usage
这是字符串生成器的正确用法(尽管您的代码看起来有问题)
请注意,如果您想要链接中断而不是使用空格,则可以使用 AppendLine。
您还可以使用 AppendFormat ,它相当于 string.format 例如
This is the correct use of a string builder (although your code looks buggy)
Note you can use AppendLine if you want a link break instead of using spaces.
You can also use AppendFormat which is the equivalent of string.format eg
像这样对集合类的 ToString() 重写在实践中很少能取得很好的效果。当集合中有数千个元素时,它们的表现不佳。一个不错的可视化是显示顶部元素和元素数量。例如:
ToString() overrides like this for a collection class rarely work out well in practice. They don't behave well when you've got thousands of elements in the collection. A decent visualization is to display the top element and the number of elements. For example: