Split a String to Array of Strings and Include the Delimiters using .NET C#

To also get the delimiter of the string after the split we’ll have to use Regex.Split. Look at the following code:
string input = "SomeText,SomeText,SomeText,SomeText,SomeText,SomeText"; 
string pattern = "(,)";
string[] substrings = Regex.Split(input, pattern);

OUTPUT
SomeText
,
SomeText
,
SomeText
,
SomeText
,
SomeText
,
SomeText

NOTE: If you remove the parentheses from the pattern, using just “,”, the delimiters will not be preserved.

You can get more info about Regex.Split here

Advertisements

2 thoughts on “Split a String to Array of Strings and Include the Delimiters using .NET C#

  1. electronic signature FAQ April 18, 2014 / 12:49

    Wonderful post however , I was wondering if you could write a litte more on this topic?
    I’d be very thankful if you could elaborate a little bit more. Appreciate it!

    Like

  2. PIYUSH SINGH April 20, 2014 / 03:14

    Is there anything in particular that you'ld like me to mention here?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s