You have a C# string you want to Split on a delimiter character, but that delimiter character also occurs in places where it is escaped. Deal with your delimiter character. Here we use a method to Split a string with escaped characters.
Input: Value1,Value2\,Value2B\,Value2C,Value3 Split field 1: Value1 Split field 2: Value2,Value2B,Value2C Split field 3: Value3
First, we know that we need to treat an escaped separator differently. One way to do this is to use Replace to substitute in a different character for the escaped character sequence. The following example code shows how an input string with a comma delimiter and escaped commas can be split correctly.
using System;
class Program
{
static void Main()
{
// Input string with escaped commas and comma delimiters
string input = @"Value1,Value2\,Value2B\,Value2C,Value3";
// Demonstrate Split A
Console.WriteLine("=== Split A ===");
foreach (string s in SplitA(input, ','))
{
Console.WriteLine(s);
}
// Demonstrate Split B
Console.WriteLine("=== Split B ===");
foreach (string s in SplitB(input, ','))
{
Console.WriteLine(s);
}
}
/// <summary>
/// Use the default split.
/// </summary>
static string[] SplitA(string value, char delimiter)
{
return value.Split(delimiter);
}
/// <summary>
/// Use string replacing to handle an escaped comma, preceded with \, correctly.
/// </summary>
static string[] SplitB(string value, char delimiter)
{
value = value.Replace("\\" + delimiter, "*"); // Replace escaped commas.
string[] parts = value.Split(delimiter); // Split on comma.
string[] result = new string[parts.Length]; // Array that stores results.
for (int i = 0; i < result.Length; i++) // Loop through new array.
{
result[i] = parts[i].Replace('*', ','); // Replace substitution character.
}
return result; // Return result.
}
}Description of the above code. The first method Main calls the following two methods and prints their results. The first output section will show the results of Split A, and the second section will show the results of Split B.
Split A. This method uses the char delimiter, in this example a comma, and results all the parts of the input string between commas. However, it will divide the middle field of the input string into three separate parts. It cannot recognize that \, is not the same as ,.
Split B. This method has additional logic in it. First it replaces the string "\," with a substitution character. In this case, it uses a star, asterisk. It then uses Split the same was as the previous method. Finally, it copies the result of Split into a new array. It replaces the substitution character with the original comma.
(See Replace String Examples.)
Results of the program execution. Here we see what the program will display when you run it. The first part shows that Split A divided the input into five strings. The second part shows that Split B divided the input into three strings, which is our desired result.
=== Split A === Value1 Value2\ Value2B\ Value2C Value3 === Split B === Value1 Value2,Value2B,Value2C Value3
Here we saw an example method that handles escaped delimiters in strings correctly. The example code can be easily adapted to handle other escape sequences, not just commas.