连接对象的字符串表示的好方法?

发布于 2024-08-08 22:22:28 字数 679 浏览 9 评论 0原文

好的,

我们的代码中有很多 where 子句。我们有多种方法来生成表示 in 条件的字符串。我试图想出一种干净的方法,如下所示:

public static string Join<T>(this IEnumerable<T> items, string separator)
{
    var strings = from item in items select item.ToString();
    return string.Join(separator, strings.ToArray());
}

它可以按如下方式使用:

var values = new []{1, 2, 3, 4, 5, 6};
values.StringJoin(",");
// result should be:
// "1,2,3,4,5,6"

所以这是一个很好的扩展方法,可以完成非常基本的工作。我知道简单的代码并不总是能够快速或高效地执行,但我只是好奇这个简单的代码可能会错过什么。我们团队的其他成员认为:

  • 它不够灵活(无法控制字符串表示)
  • 可能内存效率不高
  • 可能速度不快

有专家插话吗?

问候,

埃里克。

Ok,

We have a lot of where clauses in our code. We have just as many ways to generate a string to represent the in condition. I am trying to come up with a clean way as follows:

public static string Join<T>(this IEnumerable<T> items, string separator)
{
    var strings = from item in items select item.ToString();
    return string.Join(separator, strings.ToArray());
}

it can be used as follows:

var values = new []{1, 2, 3, 4, 5, 6};
values.StringJoin(",");
// result should be:
// "1,2,3,4,5,6"

So this is a nice extension method that does a very basic job. I know that simple code does not always turn into fast or efficient execution, but I am just curious as to what could I have missed with this simple code. Other members of our team are arguing that:

  • it is not flexible enough (no control of the string representation)
  • may not be memory efficient
  • may not be fast

Any expert to chime in?

Regards,

Eric.

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

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

发布评论

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

评论(5

情魔剑神 2024-08-15 22:22:29

关于第一个问题,您可以添加另一个“formatter”参数来控制每个项目到字符串的转换:

public static string Join<T>(this IEnumerable<T> items, string separator)
{
    return items.Join(separator, i => i.ToString());
}

public static string Join<T>(this IEnumerable<T> items, string separator, Func<T, string> formatter)
{
    return String.Join(separator, items.Select(i => formatter(i)).ToArray());
}

关于后两个问题,我不会担心它,除非您后来遇到性能问题并发现它是一个问题。然而,这不太可能成为瓶颈......

Regarding the first issue, you could add another 'formatter' parameter to control the conversion of each item into a string:

public static string Join<T>(this IEnumerable<T> items, string separator)
{
    return items.Join(separator, i => i.ToString());
}

public static string Join<T>(this IEnumerable<T> items, string separator, Func<T, string> formatter)
{
    return String.Join(separator, items.Select(i => formatter(i)).ToArray());
}

Regarding the second two issues, I wouldn't worry about it unless you later run into performance issues and find it to be a problem. It's unlikely to much of a bottleneck however...

書生途 2024-08-15 22:22:29

出于某种原因,我认为 String.Join 是根据 StringBuilder 类实现的。但如果不是,那么以下代码可能对于大型输入表现更好,因为它不会为迭代中的每个连接重新创建 String 对象。

public static string Join<T>(this IEnumerable<T> items, string separator)
{
    // TODO: check for null arguments.
    StringBuilder builder = new StringBuilder();
    foreach(T t in items)
    {
        builder.Append(t.ToString()).Append(separator);
    }

    builder.Length -= separator.Length;
    return builder.ToString();
}

编辑:这是分析何时适合使用StringBuilderString.Join

For some reason, I thought that String.Join is implemented in terms of a StringBuilder class. But if it isn't, then the following is likely to perform better for large inputs since it doesn't recreate a String object for each join in the iteration.

public static string Join<T>(this IEnumerable<T> items, string separator)
{
    // TODO: check for null arguments.
    StringBuilder builder = new StringBuilder();
    foreach(T t in items)
    {
        builder.Append(t.ToString()).Append(separator);
    }

    builder.Length -= separator.Length;
    return builder.ToString();
}

EDIT: Here is an analysis of when it is appropriate to use StringBuilder and String.Join.

孤独岁月 2024-08-15 22:22:29

为什么不使用 StringBuilder,并自己迭代集合并追加。
否则,您将创建一个字符串数组(var strings),然后进行连接。

Why don't you use StringBuilder, and iterate through the collection yourself, appending.
Otherwise you are creating an array of strings (var strings) and then doing the Join.

救星 2024-08-15 22:22:29

您缺少对序列和序列项的空检查。是的,这不是最快和最有效的内存方式。人们可能只是枚举序列并将项目的字符串表示形式呈现到 StringBuilder 中。但这真的重要吗?您遇到性能问题吗?需要优化吗?

You are missing null checks for the sequence and the items of the sequence. And yes, it is not the fastest and most memory efficient way. One would probably just enumerate the sequence and render the string representations of the items into a StringBuilder. But does this really matter? Are you experiencing performance problems? Do you need to optimize?

伴我老 2024-08-15 22:22:29

这也可以工作:

public static string Test(IEnumerable<T> items, string separator)
{
    var builder = new StringBuilder();
    bool appendSeperator = false;
    if(null != items)
    {
        foreach(var item in items)
        {
            if(appendSeperator)
            {
                builder.Append(separator)
            }

            builder.Append(item.ToString());

            appendSeperator = true;
        }
   }

   return builder.ToString();
}

this would work also:

public static string Test(IEnumerable<T> items, string separator)
{
    var builder = new StringBuilder();
    bool appendSeperator = false;
    if(null != items)
    {
        foreach(var item in items)
        {
            if(appendSeperator)
            {
                builder.Append(separator)
            }

            builder.Append(item.ToString());

            appendSeperator = true;
        }
   }

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