How to split a string using a delimiter when the delimiter can be next to each other and the first delimiter should be part of the string?
Eg:
ABC::XYZ:QUI
The split should be:
1) ABC:
2) XYZ
3) QUI
Split(':')
does not works.
You may split on the regex pattern :(?!:)
:
string input = "ABC::XYZ:QUI";
string[] parts = Regex.Split(input, @":(?!:)");
foreach (string part in parts)
{
Console.WriteLine(part);
}
This prints:
ABC:
XYZ
QUI
The regex here uses a negative lookahead to ensure that we only split/consume on :
which is not followed by another :
.