I have the following string:
string x = "hello;there;;you;;;!;"
You need to use a regex based split:
var s = "hello;there;;you;;;!;"; var res = Regex.Split(s, @";(?!;)").Where(m => !string.IsNullOrEmpty(m)); Console.WriteLine(string.Join(", ", res)); // => hello, there;, you;;, !
See the C# demo
;(?!;) regex matches any
; that is not followed with
To also avoid matching a
; at the end of the string (and thus keep it attached to the last item in the resulting list) use
$ matches the end of string (can be replaced with
\z if the very end of the string should be checked for).