-1

Possible Duplicate:
C# split string but keep split chars / separators

Is there a simple way to do a .Net string split() function that will leave the original split characters in the results?

Such that:

"some text {that|or} another".Split('{','|','}'); 

would result in an array with:

[0] = "some text "
[1] = "{"
[2] = "that"
[3] = "|"
...

Preferably without a regex.

Community
  • 1
  • 1
Brady Moritz
  • 8,624
  • 8
  • 66
  • 100
  • http://stackoverflow.com/questions/2484919/how-do-i-split-a-string-by-strings-and-include-the-delimiters-using-net @TimSchmelter not all of those answers are regex implementations. – D3vtr0n Nov 14 '12 at 22:52
  • good solutions at that link.. thx – Brady Moritz Nov 15 '12 at 00:51

2 Answers2

3

check out this post

the first answer with a RegEx solution, the second for a non-regex solution...

In Concept...

string source = "123xx456yy789";
foreach (string delimiter in delimiters)
    source = source.Replace(delimiter, ";" + delimiter + ";");
string[] parts = source.Split(';');
Community
  • 1
  • 1
adam
  • 2,930
  • 7
  • 54
  • 89
0

you can probably roll your own using String.IndexOf Method (String, Int32) to find all of your initial separator characters, and merge those in with the results of String.Split