I would like to split string "203136;70;;;;;"
by ;;
separator but in this string separator was mixed with regular symbol ;
From "203136;70;;;;;"
To
- 203136;70
- ;
I would like to split string "203136;70;;;;;"
by ;;
separator but in this string separator was mixed with regular symbol ;
From "203136;70;;;;;"
To
You can use StringSplitOptions.RemoveEmptyEntries
:
using System;
class Program
{
static void Main()
{
string input = "203136;70;;;;;";
string[] result = input.Split(new string[] { ";;" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string r in result)
{
Console.WriteLine(r);
}
}
}
203136;70
;
string[] SplitValuesLine(string line)
{
var list = new List<string>();
var charArray = line.ToCharArray();
var builder = new StringBuilder();
var valueReached = false;
for (int i = 0; i < charArray.Length; i++)
{
if (valueReached && (i + 1) < charArray.Length && charArray[i] == charArray[i + 1] && charArray[i] == ';')
{
list.Add(builder.ToString());
builder.Clear();
valueReached = false;
i++;
}
else
{
builder.Append(charArray[i]);
valueReached = true;
}
}
return list.ToArray();
}
This not elegant solution helped me, cause the StringSplitOptions.RemoveEmptyEntries
removes some strings which I would like to have.