StringBuilder相关问题

发布于 2024-08-28 15:01:13 字数 872 浏览 3 评论 0原文

我已经为堆栈编写了一个程序。 (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 技术交流群。

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

发布评论

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

评论(4

ぃ双果 2024-09-04 15:01:13

您可以使用 Array.ConvertAllString.Join 而不是自己迭代列表。

另外,当你在一行中谈论多个事情时......你在任何地方都没有任何换行符。

或者,如果您继续使用 StringBuilder,Append 方法将返回 StringBuilder,以便您可以将调用链接在一起:

 sb.Append("(top: ").Append(top).Append(")").AppendLine();

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:

 sb.Append("(top: ").Append(top).Append(")").AppendLine();
ˇ宁静的妩媚 2024-09-04 15:01:13

您可以使用这样的扩展方法来汇总可枚举集合

/// <summary>
/// A better ToString for Enumerable objects (mostly for logging)
/// </summary>
public static string ToStringList(this IEnumerable<string> collection, int limit)
{
    return string.Join(", ", collection.Take(limit));
}

用法

string result = tabel.Select(x => x.ToString()).ToStringList(50);

PS 如果您使用 .NET 版本 4 之前的版本,您可能需要一个 .ToArray() 来满足 string.Join()

或者,更好的是,使用重载: string Join(string seperator, IEnumerablevalues); 您可以简化为:-

/// <summary>
/// A better ToString for Enumerable objects (mostly for logging)
/// </summary>
public static string ToStringList<T>(this IEnumerable<T> collection, int limit)
{
    return string.Join(", ", collection.Take(limit));
}

用法

string result = tabel.ToStringList(50);

You could use an extension method like this to summarize enumerable collections

/// <summary>
/// A better ToString for Enumerable objects (mostly for logging)
/// </summary>
public static string ToStringList(this IEnumerable<string> collection, int limit)
{
    return string.Join(", ", collection.Take(limit));
}

Usage

string result = tabel.Select(x => x.ToString()).ToStringList(50);

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:-

/// <summary>
/// A better ToString for Enumerable objects (mostly for logging)
/// </summary>
public static string ToStringList<T>(this IEnumerable<T> collection, int limit)
{
    return string.Join(", ", collection.Take(limit));
}

Usage

string result = tabel.ToStringList(50);
故事灯 2024-09-04 15:01:13

这是字符串生成器的正确用法(尽管您的代码看起来有问题)

请注意,如果您想要链接中断而不是使用空格,则可以使用 AppendLine。

您还可以使用 AppendFormat ,它相当于 string.format 例如

builder.AppendFormat("(top:{0})", value);

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

builder.AppendFormat("(top:{0})", value);
多像笑话 2024-09-04 15:01:13

像这样对集合类的 ToString() 重写在实践中很少能取得很好的效果。当集合中有数千个元素时,它们的表现不佳。一个不错的可视化是显示顶部元素和元素数量。例如:

public override string ToString() {
  if (this.Count == 0) return "Empty";
  else return string.Format("Top:{0}, Count:{1}", top, Count);
}

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:

public override string ToString() {
  if (this.Count == 0) return "Empty";
  else return string.Format("Top:{0}, Count:{1}", top, Count);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文