My task is simple: I have a CSV file inside a C# string, split with semicolons. I need to add spaces for each empty cell. A;B;;;;C;
should become A;B; ; ; ;C;
. Right now, I'm using the replace method twice:
csv = csv.Replace(";;", "; ;").Replace(";;", "; ;");
That's necessary, because in the first pass, it will replace any occurance of ;; with a space between, but there's no lookback, so the second semicolon of the replaced sequence won't be checked again. Therefore I would end up with a A;B; ;; ;C;
, which is not what I want.
Is there a more elegant, clear, and less redundand way to solve that task?
You can try to Split
string into the parts, then replace empty entries with spaces using Select
(it requires using System.Linq;
) and Join
the entries back
var str = "A;B;;;;C";
var parts = str.Split(';').Select(p => string.IsNullOrEmpty(p) ? " " : p);
var result = string.Join(";", parts);
The output will be the following A;B; ; ; ;C
Benchmark result in comparison with OP code and Regex
solution:
What is the clear and more elegant is up to you decision. Benchmark code for the reference is below
[SimpleJob]
public class Benchmark
{
string input= "A;B;;;;C";
[Benchmark]
public string SplitJoinTest()
{
var parts = input.Split(';').Select(p => string.IsNullOrEmpty(p) ? " " : p);
return string.Join(";", parts);
}
[Benchmark]
public string DoubleReplaceTest()
{
return input.Replace(";;", "; ;").Replace(";;", "; ;");
}
[Benchmark]
public string RegexTest()
{
return Regex.Replace(input, ";(?=;)", "; ");
}
}
One way is to use regular expressions.
using System.Text.RegularExpressions;
var result = Regex.Replace("A;B;;;;C;", ";(?=;)", "; ");
We replace every semicolon that is followed by another semicolon with the string "; "
.
It's definitely less redundant, and it's clear if you know how to read regex :) Whether it is more elegant is up to you to decide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With