如果当前行以 SPACE 开头,则删除前面的 CRLF
Remove preceeding CRLF if the current line starts with a SPACE
我需要使用文本文件的示例源:
Analysis: this is a test that wraps
and this is the second line
this is the third line
Demonstration: This is an example of only one line
Result data more text
如果一切正常,我的预期输出:
线路编号
来源
描述
1
分析:这是一个循环测试这是第二行这是第三行
2
演示:这是只有一行的例子
3
结果数据更多文字
这是我要添加到数据网格视图的代码
string path = @"Sample1.txt";
dtSource.Columns.Add("LineID");
dtSource.Columns.Add("Source");
dtSource.Columns.Add("Description");
string[] readText = File.ReadAllLines(path);
int linenum = 0;
foreach (string s in readText)
{
string sourceline = s;
if(sourceline.StartsWith(" "))
{
sourceline = sourceline.Replace("\n", "").Replace("\r", ""); //STUCK
}
dtSource.Rows.Add(linenum, sourceline, "");
linenum++;
}
dataGridView1.DataSource = dtSource;
现在想不清楚了。正在考虑仅使用 List<string>
然后进行操作,但不想添加更多内容。此外,考虑只修改连接两者的前一个数据表行中的文本。任何 linq 查询或我可以做些什么来简化这个?
这应该有效
foreach (string s in readText)
{
string sourceline = s;
if (sourceline.StartsWith(" "))
{
dtSource.Rows[linenum - 1]["Source"] += sourceline;
}
else
{
dtSource.Rows.Add(linenum, sourceline, "");
linenum++;
}
}
您可以使用Aggregate
var result = readText.Aggregate(new List<string>(), (acc, x) =>
{
if (x.StartsWith(" "))
{
acc[acc.Count - 1] += x;
}
else
{
acc.Add(x);
}
return acc;
}).Select((s, i) => dtSource.Rows.Add(i + 1, s, string.Empty)).ToList();
我需要使用文本文件的示例源:
Analysis: this is a test that wraps
and this is the second line
this is the third line
Demonstration: This is an example of only one line
Result data more text
如果一切正常,我的预期输出:
线路编号 | 来源 | 描述 |
---|---|---|
1 | 分析:这是一个循环测试这是第二行这是第三行 | |
2 | 演示:这是只有一行的例子 | |
3 | 结果数据更多文字 |
这是我要添加到数据网格视图的代码
string path = @"Sample1.txt";
dtSource.Columns.Add("LineID");
dtSource.Columns.Add("Source");
dtSource.Columns.Add("Description");
string[] readText = File.ReadAllLines(path);
int linenum = 0;
foreach (string s in readText)
{
string sourceline = s;
if(sourceline.StartsWith(" "))
{
sourceline = sourceline.Replace("\n", "").Replace("\r", ""); //STUCK
}
dtSource.Rows.Add(linenum, sourceline, "");
linenum++;
}
dataGridView1.DataSource = dtSource;
现在想不清楚了。正在考虑仅使用 List<string>
然后进行操作,但不想添加更多内容。此外,考虑只修改连接两者的前一个数据表行中的文本。任何 linq 查询或我可以做些什么来简化这个?
这应该有效
foreach (string s in readText)
{
string sourceline = s;
if (sourceline.StartsWith(" "))
{
dtSource.Rows[linenum - 1]["Source"] += sourceline;
}
else
{
dtSource.Rows.Add(linenum, sourceline, "");
linenum++;
}
}
您可以使用Aggregate
var result = readText.Aggregate(new List<string>(), (acc, x) =>
{
if (x.StartsWith(" "))
{
acc[acc.Count - 1] += x;
}
else
{
acc.Add(x);
}
return acc;
}).Select((s, i) => dtSource.Rows.Add(i + 1, s, string.Empty)).ToList();