19

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.

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. How can I make this code work as described above?

1
  • Can Some text include " - " in it? For instance, can your input string look like this ABC - DEF - 04.09.1996 - 40-18? Commented Aug 16, 2016 at 13:33

5 Answers 5

29

You should just split with spaces around -:

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

See C# demo

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
Sign up to request clarification or add additional context in comments.

2 Comments

Is RemoveEmptyEntries really important here?
@Rawling: You can use StringSplitOptions.None if you need empty values, too. Judging by the current task, StringSplitOptions.RemoveEmptyEntries should be OK. If you wonder if there is a string.Split overload with only 1 argument, no, all the overloads allowing a string require the StringSplitOptions argument.
16

Use this overload of string split to only get 3 parts:

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());

1 Comment

The second option here is the only answer that works correctly if the formatting is not fixed, i.e. if the number of spaces around the hyphens can vary.
3

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. 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];

Comments

1

In current case you can use Split with extra space like

string.Split(" - ")

In term of "good practice" can't recommend this solution.

8 Comments

Please use code formatting, not quoting for code blocks.
Thanks DavidG, will do
Also as a general rule, if your answer is the same as someone else's, don't post, but upvote the other answer :) If you have somethign to add, a comment or edit suggestion is always welcome
I would love to know why this is not considered good practice, and what would make it better.
Is not a good practice because split by "blank" spaces is very dogie. As you can see, in example above you can have "34-35" or "text - " and many others examples. In this case, this solution works, in others it wont, and after a while, when problem occurs, is hard to find the problem. So, I'd say, the optimal solution is in above example, where you remove spaces and split string only in 3 parts. P.S. I wrote my solution in same time with guys above.
|
0

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"

1 Comment

Does this answer the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.