简体   繁体   English

在 C# 中手动拆分字符串

[英]Manual string split in C#

In my code, I am attempting to manipulate a string:在我的代码中,我试图操作一个字符串:

Some text - 04.09.1996 - 40-18

I'd like to split this into three substrings: Some text , 04.09.1996 , and 40-18 .我想将其拆分为三个子字符串: Some text04.09.199640-18

When I use the Split method with a hyphen as a separator, the return value is an array of four strings: Some text , 04.09.1996 , 40 , and 18 .当我使用带有连字符作为分隔符的Split方法时,返回值是一个包含四个字符串的数组: Some text04.09.19964018 How can I make this code work as described above?如何使此代码按上述方式工作?

You should just split with spaces around - :你应该用空格分开-

 .Split(new[] {" - "}, StringSplitOptions.RemoveEmptyEntries);

See C# demo参见C# 演示

var res = "Some text - 04.09.1996 - 40-18".Split(new[] {" - "}, StringSplitOptions.RemoveEmptyEntries);
foreach (var s in res)
    Console.WriteLine(s);

Result:结果:

Some text
04.09.1996
40-18

Use this overload of string split to only get 3 parts:使用此字符串拆分重载仅获得 3 个部分:

var s = "Some text - 04.09.1996 - 40-18";
var parts = s.Split(new[] { '-' }, 3);

I'm assuming you also want to trim the spaces too:我假设您也想修剪空格:

var parts = s.Split(new[] { '-' }, 3)
    .Select(p => p.Trim());

I would be wary of "-" or " - " appearing in "Some text", as I assume that you are interested in that as a place holder.我会警惕出现在“某些文本”中的“-”或“-”,因为我假设您对此感兴趣作为占位符。 If you are certain that "Some text" will not contain "-" than the other answers here are good, simple and readable.如果您确定“某些文本”不包含“-”,那么这里的其他答案都很好,简单易读。 Otherwise we need to rely on something that we know is constant about the string.否则,我们需要依赖我们知道的字符串常量。 It looks to me like the thing that is constant is the last 3 hyphens.在我看来,不变的是最后 3 个连字符。 So I would try split on "-" and put the last pair back together like所以我会尝试拆分“-”并将最后一对重新组合在一起,就像

string input = "Some text - 04.09.1996 - 40-18";
string[] foo = input.Split(new[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
int length = foo.Length;
string[] bar = new string[3];

//put "some text" back together
for(int i=0; i< length - 3;i++)
{
   bar[0] += foo[i];
}

bar[1] = foo[length - 3];
bar[2] = foo[length - 2] + "-" + foo[length - 1];

In current case you can use Split with extra space like在当前情况下,您可以使用 Split 和额外的空间,例如

string.Split(" - ")

In term of "good practice" can't recommend this solution.就“良好做法”而言,不能推荐此解决方案。

I am replaced character sequence '--------------------' in your string to special character "&" like below.我将字符串中的字符序列 '--------------------' 替换为特殊字符“&”,如下所示。 and then split using special character "&"然后使用特殊字符“&”分割

 string str = "Hello, my- name --------------------  is Philip J. Fry -------------------- and i like cartoons".Replace("--------------------","&");
  string[] ss=str.Split('&');
  string result=ss[0] + "," + ss[1]+ "," +ss[2];

then output string looks like "Hello, my- name, is Philip J. Fry, and i like cartoons"然后输出字符串看起来像“你好,我的名字,是 Philip J. Fry,我喜欢卡通”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM