I'd like to modify a source string which is looking like
"one.two.three" 
and transfer it into a string with slashes to use it as a folder string which has the following structure:
"one\one.two\one.two.three"
Do you know more elegant ways to realize this, than my solution below? I'm not very satisfied with my for-loops.
var folder = "one.two.three";
var folderParts = folder.Split('.');
var newFolder = new StringBuilder();
for (int i = 0; i < folderParts.Length; i++)
{
    for (int j = 0; j < i; j++)
    {
       if (j == 0)
       {
          newFolder.Append("\\");
       }
       newFolder.Append($"{folderParts[j]}.");
    }
    newFolder.Append(folderParts[i]);
}
                You can do this quite tersely with Regex
var newFolder = Regex.Replace(folder, @"\.", @"\$`.");
This matches on each period. Each time it finds a period, it inserts a backslash and then the entire input string before the match ($`). We have to add the period in again at the end.
So, steps are (< and > indicate text inserted by the substitution at that step):
one<\one>.two.three
one\one.two<\one.two>.three
one\one.two\one.two.three
For bonus points, use Path.DirectorySeparatorChar for cross-platform correctness.
var newFolder = Regex.Replace(folder, @"\.", $"{Path.DirectorySeparatorChar}$`.")
Here's another linqy way:
var a = "";
var newFolder = Path.Combine(folder.Split('.')
    .Select(x => a += (a == "" ? "" : ".") + x).ToArray());
                        You can try Linq:
  string folder = "one.two.three";
  string[] parts = folder.Split('.');
  string result = Path.Combine(Enumerable
    .Range(1, parts.Length)
    .Select(i => string.Join(".", parts.Take(i)))
    .ToArray());
  Console.Write(newFolder);
Outcome:
 one\one.two\one.two.three 
                        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