RJV
RJV

Reputation: 287

Split string by character in C#

I need to split this code by ',' in C#.

Sample string:

'DC0''008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'

I can use string.split(',') but as you can see 'Comm,erc,' is split up by

comm
erc

also 'DC0''008_' should split up as

'DC0''008_'

not as

'DC0'

'008_'

The expected output should be like this:

'DC0''008_'

'23802.76'

'23802.76'

'23802.76'

'Comm,erc,'

'2f17'

'3f44c0ba-daf1-44f0-a361-'

Upvotes: 1

Views: 149

Answers (4)

anubhava
anubhava

Reputation: 784918

split can do it but regex will be more complex.

You can use Regex.Matches using this simpler regex:

'[^']*'

and get all quoted strings in a collection.

Code:

MatchCollection matches = Regex.Matches(input, @"'[^']*'");

To print all the matched values:

foreach (Match match in Regex.Matches(input, @"'[^']*'"))
         Console.WriteLine("Found {0}", match.Value);

To store all matched values in an ArrayList:

ArrayList list = new ArrayList();
foreach (Match match in Regex.Matches(input, @"'[^']*'")) {
   list.add(match.Value);
}

EDIT: As per comments below if OP wants to consume '' in the captured string then use this lookaround regex:

'.*?(?<!')'(?!')

(?<!')'(?!') means match a single quote that is not surrounded by another single quote.

RegEx Demo

Upvotes: 3

Abion47
Abion47

Reputation: 24616

You can use this Regex to get all the things inside the commas and apostrophes:

(?<=')[^,].*?(?=')

Regex101 Explanation

To convert it into a string array, you can use the following:

var matches = Regex.Matches(strInput, "(?<=')[^,].*?(?=')");
var array = matches.Cast<Match>().Select(x => x.Value).ToArray();

EDIT: If you want it to be able to capture double quotes, then the Regex that will match it in every case becomes unwieldy. At this point, It's better to just use a simpler pattern with Regex.Split:

var matches = Regex.Split(strInput, "^'|'$|','")
                   .Where(x => !string.IsNullOrEmpty(x))
                   .ToArray();

Upvotes: 2

Harminder
Harminder

Reputation: 141

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "'DC0008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'";
            var matches = Regex.Matches(str, "(?<=')[^,].*?(?=')");
            var array = matches.Cast<Match>().Select(x => x.Value).ToArray();
            foreach (var item in array)
            Console.WriteLine("'" + item + "'");
        }
    }
}

Upvotes: 0

Zain Ul Abidin
Zain Ul Abidin

Reputation: 2690

it is good to modify your string then split it so that you will achieve what you want like some thing below

string data = "'DC0008_','23802.76','23802.76','23802.76','Comm,erc,','2f17','3f44c0ba-daf1-44f0-a361-'";

data = Process(data); //process before split i.e for the time being replace outer comma with some thing else like '@'

string[] result = data.Split('@'); // now it will work lolz not confirmed and tested

the Process() function is below

private string Process(string input)
{
   bool flag = false;
   string temp="";
   char[] data = input.ToCharArray();
   foreach(char ch in data)
   {
     if(ch == '\'' || ch == '"')
         if(flag)
           flag=false;
         else
           flag=true;

      if(ch == ',')
       {
           if(flag) //if it is inside ignore else replace with @
             temp+=ch;
           else
             temp+="@";
       }
      else
          temp+=ch;
   }
 return temp;
}

see output here http://rextester.com/COAH43918

Upvotes: 0

Related Questions