【发布时间】:2019-03-22 08:26:54
【问题描述】:
我想修改一个看起来像
的源字符串"one.two.three"
并将其转换为带有斜线的字符串,以将其用作具有以下结构的文件夹字符串:
"one\one.two\one.two.three"
你知道比我下面的解决方案更优雅的方法吗?我对我的 for 循环不太满意。
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]);
}
【问题讨论】:
-
got 可以通过 Aggregate 实现这一点,但我不能完全确定。