String.Join 和 String.Split 参数助记符

发布于 2024-10-21 22:01:00 字数 899 浏览 1 评论 0原文

我每次都会遇到 C# 的 String.Join 和 String.Split 参数类型。问题是 String.Split 采用 params char[] 字符数组,而 String.Join 采用 string >。

有没有人有一个好的助记符可以帮助我记住哪个需要哪个,这样我就不必每次进行字符串操作时都修复这个问题?

[编辑,因为每个人似乎都很困惑为什么我没有 IDE 支持]

我正在使用 LinqPad,就像这样,当我大多数时间使用它时:

String.Join("\n", @"LongRawString
WithPlentyOfLines
UsuallyGeneratedBySomeoneElse
OrProducedBySqlServerForExample".Split('\n').Select(x => 
    {
        x = x.Trim();

        //create line of code, like:
        return "int longRawStringIdx = reader.GetOrdinal(\"LongRawString\")";
    }))

我没有得到 IDE 支持在 LinqPad 中,并且希望节省我每次返回并修复它所花费的几秒钟。这很愚蠢,但我们程序员为自动化每天执行的 12 秒任务而编写的脚本中有一半也是如此。

问题是我搞砸了是否应该给予SplitstringJoinstring code> 作为其第一个参数(或仅在 Split 的情况下)。

I trip on C#'s String.Join and String.Split parameter types every time. The problem is that String.Split takes a params char[] array of characters and String.Join takes a string.

Does anyone have a good mnemonic that can help me remember which takes which so I do not have to fix this every single time I do a string manipulation?

[EDIT, since everyone seems to be confused why I don't have IDE support]

I'm using LinqPad, like this, when I use this most of the time:

String.Join("\n", @"LongRawString
WithPlentyOfLines
UsuallyGeneratedBySomeoneElse
OrProducedBySqlServerForExample".Split('\n').Select(x => 
    {
        x = x.Trim();

        //create line of code, like:
        return "int longRawStringIdx = reader.GetOrdinal(\"LongRawString\")";
    }))

I don't get IDE support in LinqPad, and would like to save the few seconds it takes me to go back and fix it every time. It's silly, but then so are half the scripts that we programmers write to automate the 12 second tasks we do every day.

The problem is that I screw up whether or not I'm supposed to be giving Split the string or Join the string as its first parameter (or only in the case of Split).

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

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

发布评论

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

评论(1

少女七分熟 2024-10-28 22:01:00

您可以这样想:您指定要连接的一个事物,但要指定几个可能的分隔符。这样是有意义的,因为输入字符串可能已经有许多分隔符,但加入多个分隔符是没有意义的,因为 Join 方法必须计算出每个分隔符的含义时间。

不过我只想使用 Intellisense :)

You could think of it this way: you're specifying one thing to join on, but several possible delimiters to split on. It makes sense that way round as the input string could already have many delimiters, but it doesn't make sense to join on multiple delimiters, as the Join method would have to work out which delimiter you meant each time.

I'd just use Intellisense though :)

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