James Cotter
James Cotter

Reputation: 808

Split on multiple characters while keeping the character in the result

I want to split on '0' and '1' while keeping those characters in the split result. How can I do this in C#?

e.g.

"012345" => 0,1,2345

I tried

Regex.Split(number, @"(?=[01])");

but I get "", 0, 12345 as the result

The following seems to work, except for "" in between splits.

Regex.Split(number, @"(0|1)");

Upvotes: 3

Views: 497

Answers (1)

Adam S
Adam S

Reputation: 3125

You could use LINQ to simply exclude the empty elements using the regex pattern you mentioned in your post:

var results = Regex.Split(number, @"(0|1)")
                   .Where(p => !String.IsNullOrEmpty(p));

This could also work. I'd like to see a more elegant approach, which I feel is what you are seeking, but it gets the job done.

List<string> results = new List<string>();
int curInd = 0;

var matchInfo = Regex.Matches(number, "(0|1)");
foreach (Match m in matchInfo)
{
    //Currently at the start of a match, add the match sequence to the result list.
    if (curInd == m.Index)
    {
        results.Add(number.Substring(m.Index, m.Length));
        curInd += m.Length;  
    }
    else  //add the substring up to the match point and then add the match itself
    {
        results.Add(number.Substring(curInd, m.Index - curInd));
        results.Add(number.Substring(m.Index, m.Length));
        curInd = m.Index + m.Length;
    }
}
//add any remaining text after the last match
if (curInd < number.Length)
{
    results.Add(number.Substring(curInd));
}

Upvotes: 1

Related Questions