.Net Split with split characters retained [duplica

2019-09-22 02:48发布

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.

2条回答
成全新的幸福
2楼-- · 2019-09-22 03:22

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(';');
查看更多
祖国的老花朵
3楼-- · 2019-09-22 03:29

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

查看更多
登录 后发表回答