String.Join 和 String.Split 参数助记符
我每次都会遇到 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 秒任务而编写的脚本中有一半也是如此。
问题是我搞砸了是否应该给予Split
string
或Join
string
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样想:您指定要连接的一个事物,但要指定几个可能的分隔符。这样是有意义的,因为输入字符串可能已经有许多分隔符,但加入多个分隔符是没有意义的,因为
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 :)