String.Split 仅在 C# 中的第一个分隔符上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2019738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String.Split only on first separator in C#?
提问by Anonym
String.Split is convenient for splitting a string with in multiple part on a delimiter.
String.Split 便于在分隔符上拆分多个部分的字符串。
How should I go on splitting a string only on the first delimiter. E.g. I've a String
我应该如何继续仅在第一个分隔符上拆分字符串。例如我有一个字符串
"Time: 10:12:12\r\n"
And I'd want an array looking like
我想要一个看起来像的数组
{"Time","10:12:12\r\n"}
采纳答案by Thorarin
The best approach depends a little on how flexible you want the parsing to be, with regard to possible extra spaces and such. Check the exact format specifications to see what you need.
最好的方法在一定程度上取决于您希望解析的灵活性,以及可能的额外空间等。检查确切的格式规范,看看你需要什么。
yourString.Split(new char[] { ':' }, 2)
Will limit you two 2 substrings. However, this does not trim the space at the beginning of the second string. You could do that in a second operation after the split however.
将限制你两个 2 子串。但是,这不会修剪第二个字符串开头的空格。但是,您可以在拆分后的第二个操作中执行此操作。
yourString.Split(new char[] { ':', ' ' }, 2,
StringSplitOptions.RemoveEmptyEntries)
Should work, but will break if you're trying to split a header name that contains a space.
应该可以工作,但如果您尝试拆分包含空格的标题名称,则会中断。
yourString.Split(new string[] { ": " }, 2,
StringSplitOptions.None);
Will do exactly what you describe, but actually requires the space to be present.
将完全按照您的描述进行操作,但实际上需要存在空间。
yourString.Split(new string[] { ": ", ":" }, 2,
StringSplitOptions.None);
Makes the space optional, but you'd still have to TrimStart()
in case of more than one space.
使空格可选,但如果有TrimStart()
多个空格,您仍然必须这样做。
To keep the format somewhat flexible, and your code readable, I suggest using the first option:
为了保持格式有点灵活,并且您的代码可读,我建议使用第一个选项:
string[] split = yourString.Split(new char[] { ':' }, 2);
// Optionally check split.Length here
split[1] = split[1].TrimStart();
回答by Martin Peck
In your example above you could split on ": " (i.e. colon with trailing space) as this appears to be what you've done. If you really did split on just the first delimeter you'd see a leading space in your second array element.
在上面的示例中,您可以拆分“:”(即带有尾随空格的冒号),因为这似乎是您所做的。如果你真的只在第一个分隔符上拆分,你会在第二个数组元素中看到一个前导空格。
However, you should probably look at this overload of Split...
但是,您可能应该看看 Split 的重载...
http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
public string[] Split(
char[] separator,
int count
)
... which allows you to specify a max number of substrings.
...它允许您指定最大数量的子字符串。
回答by serhio
?("Time: 10:12:12\r\n").Split(new char[] { ':', ' ' }, 2,
StringSplitOptions.RemoveEmptyEntries)
{Dimensions:[2]}
[0]: "Time"
[1]: "10:12:12\r\n"
other options:
其他选项:
?("Time: 10:12:12\r\n").Split(new char[] { ':' }, 2)
{Dimensions:[2]}
[0]: "Time"
[1]: " 10:12:12\r\n"
?("Time: 10:12:12\r\n").Split(new char[] { ':' }, 1)
{Dimensions:[1]}
[0]: "Time: 10:12:12\r\n"
?("Time: 10:12:12\r\n").Split(new char[] { ':' }, 3)
{Dimensions:[3]}
[0]: "Time"
[1]: " 10"
[2]: "12:12\r\n"
回答by spinalfrontier
I've adopted a variation to Thorarin's answer above, The below should be able to handle your requirement, plus trim the spaces.
我对上面的 Thorarin 的回答采用了一个变体,下面的应该能够满足您的要求,并修剪空格。
yourString.Split(new []{'-'},2).Select(s => s.Trim())