c# - 使用列表动态写入文件:格式问题
问题描述
我写了一个函数,它可以让我动态地编写一个包含一些代码的文件。我正在为方法提供点数。我已经搞定了,但我遇到了一个愚蠢的格式问题。
以下代码(带numberOfPoints = 3
)
private static void PointsGenerator(int numberOfPoints)
{
string fileContent = Resources.Points_AutoGenerated;
List<string> lines = new List<string>(fileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
for (var j = 0; j < numberOfPoints - 1; j++)
{
int index = 0;
for (var i = 1; !lines[i].Equals("ENDMODULE"); i++)
{
if (lines[i].Contains("! <<< POINT PLACEHOLDER >>>"))
{
if (numberOfPoints == 1)
{
// Let's remove the placeholder...
lines.RemoveAt(i);
// ... and the extra new-line.
lines.RemoveAt(i);
}
else
{
lines.RemoveAt(i);
lines.Insert(i, Resources.Snippet_POINT);
index = i;
}
}
}
if (j < numberOfPoints - 2)
{
lines.Insert(index + 2, "! <<< POINT PLACEHOLDER >>>");
}
}
Console.WriteLine(string.Join(Environment.NewLine, lines.ToArray()));
}
正在输出这个文件
! <<< Code generated with MG ABB Missions Generator. >>>
MODULE Points_AutoGenerated
TASK PERS robtarget pPointX := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
ENDMODULE
我不喜欢它,因为我在该行之前缺少了一个额外的ENDMODULE
换行符。
但是如果我使用第二个代码(只是粘贴我改变的部分)
if (j < numberOfPoints - 2)
{
lines.Insert(index + 2, "! <<< POINT PLACEHOLDER >>>");
}
else
{
lines.Insert(index + 1, Environment.NewLine);
}
我得到了这个输出,而不是
! <<< Code generated with MG ABB Missions Generator. >>>
MODULE Points_AutoGenerated
TASK PERS robtarget pPointX := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
ENDMODULE
与以前类似,我不喜欢最后的换行符(我只想要其中一个!)。
我错过了什么?
这些是我用来生成最终文件的两个模板:
Points_AutoGenerated.txt
! <<< Code generated with MG ABB Missions Generator. >>>
MODULE Points_AutoGenerated
TASK PERS robtarget pPointX := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
! <<< POINT PLACEHOLDER >>>
ENDMODULE
和Snippet_POINT.txt
TASK PERS robtarget pPointY := [[0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 0], [9E9, 9E9, 9E9, 9E9, 9E9, 9E9]];
解决方案
只需添加一个空字符串,您已经从 WriteLine(Join()) 中获得了一个换行符。
else
{
lines.Insert(index + 1, "");
}
// here the empty string becomes an empty line
Console.WriteLine(string.Join(Environment.NewLine, lines.ToArray()));
推荐阅读
- vba - 无法过滤 BookingID 的号码和重复
- sharepoint - 使用 REST oData url 访问共享点子站点的列表项
- python - 在 numpy 数组中提取非零值组
- variables - (如何)在 Azure DevOps 中对变量组进行版本控制?
- css - React 条件样式组件在 chrome 中不起作用?
- angular - 类型“{}”缺少“FooModelDTO”类型的以下属性
- sharepoint-online - 嵌入式 SharePoint Online Excel 文件不显示水平滚动条
- makefile - Yocto:可以在 Yocto Makefile 中使用 ${STAGING_DIR_TARGET} 吗?
- powershell - 使用 az cli 创建 EventGrid 订阅
- java - 如何在 Redis 中使用 Jedis 中的多个键?二级索引