I wrote my Split()
extension, it's main goal is to save delimiters and add them to splitted strings.
For example:
I have a string "-1-2+3+4-5-6"
, and separators '+'
, '-'
and I want to have following: ""
,"-1"
,"-2"
,"+3"
,"+4"
,"-5"
,"-6"
or "-"
,"1-"
,"2+"
,"3+"
,"4-"
,"5-"
,"6"
depends on input separator option.
My extension method consists of three parts:
- Check input values;
- Getting all indexes of separators, and separators in source string;
- Split source string and adding separators to left or right substring.
Code:
public enum SeparatorOptions
{
None = 0,
AddSeparatorToLeftSubstring = 1,
AddSeparatorToRightSubstring = 2
}
public static class Extensions
{
public static string[] Split(this string source, char[] separators, SeparatorOptions separatorOptions)
{
if (separatorOptions < SeparatorOptions.None || separatorOptions > SeparatorOptions.AddSeparatorToRightSubstring)
{
throw new ArgumentException("Arg_SeparatorEnumIllegalVal");
}
if (separators == null || separators.Length == 0)
{
return source.Split((char[])null);
}
if (separatorOptions == SeparatorOptions.None)
{
return source.Split(separators);
}
// Getting all indexes of separators, and separators in source string
int foundCount = 0;
int[] separatorIndexes = new int[source.Length];
char[] separatorValues = new char[source.Length];
for (int i = 0; i < source.Length; i++)
for (int j = 0; j < separators.Length; j++)
{
char separator = separators[j];
if (source[i] == separator)
{
separatorValues[foundCount] = source[i];
separatorIndexes[foundCount++] = i;
break;
}
}
string[] splitString = source.Split(separators);
// Adding separators to left or right substring
if (separatorOptions == SeparatorOptions.AddSeparatorToLeftSubstring)
return AddSeparatorToLeftSubstring(splitString, separatorValues, foundCount);
if (separatorOptions == SeparatorOptions.AddSeparatorToRightSubstring)
return AddSeparatorToRightSubstring(splitString, separatorValues, foundCount);
return splitString;
}
private static string[] AddSeparatorToLeftSubstring(string[] splitString, char[] separatorValues, int foundCount)
{
for (int i = 0; i < foundCount; i++)
{
splitString[i] += separatorValues[i];
}
return splitString;
}
private static string[] AddSeparatorToRightSubstring(string[] splitString, char[] separatorValues, int foundCount)
{
for (int i = 1, j = 0; i <= foundCount; i++, j++)
{
splitString[i] = separatorValues[j] + splitString[i];
}
return splitString;
}
}
class Program
{
static void Main(string[] args)
{
string str = "-1-2+3+4-5-6";
Console.WriteLine("Original split()");
var res2 = str.Split(new char[] { '+', '-' }, SeparatorOptions.None);
foreach (var el in res2) Console.WriteLine(el);
Console.WriteLine("Add to right substring");
var res = str.Split(new char[] { '+', '-' }, SeparatorOptions.AddSeparatorToRightSubstring);
foreach (var elem in res) Console.WriteLine(elem);
Console.WriteLine("Add to left substring");
var res3 = str.Split(new char[] { '+', '-' }, SeparatorOptions.AddSeparatorToLeftSubstring);
foreach (var el in res3) Console.WriteLine(el);
Console.ReadKey();
}
}
What I wrote seems to work, although I am not sure if it is the best way, or recommended way to do something like this, so I want to ask what is wrong with my code and how could it be done better.