Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7546715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:16:16+00:00 2026-05-30T09:16:16+00:00

I want to split on ‘0’ and ‘1’ while keeping those characters in the

  • 0

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)");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:16:17+00:00Added an answer on May 30, 2026 at 9:16 am

    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));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split a string like this: abc//def//ghi into a part before and
I want to split this text in to 2 parts with C#.Net windows application.
I want to split string without using split . can anybody solve my problem
I want to split the qc column every 255 characters into multiple strings and
$data = google,98%,bing,92%,searchengine,56%,seo,85%,search,94%; I want split thsese and get final result google = 98%
I want to split a string into parts based on a regex (\$\d+\$), but
I want to split following string with a comma. 1,x1,43,tr,y,7 Result array should be
String filterPath=aa.bb.cc{k1:v1,k2:{s1:s2}},bb.cc,ee.dd; String[] result=filterPath.split(,); for(String r:result){ System.out.println(r); } I want split the String filterPath
i want to split this string by the (*) sign $string= How Many Papers*On
I want to split string abcdefgh to ab,cd,ef,gh using javascript split() abcdefgh.split(???) Can you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.