【发布时间】:2020-09-23 14:17:30
【问题描述】:
我想用 CSV 文件中的制表符替换分隔符逗号
输入
输出
请注意,引号不应替换逗号。同样在输出中,我们要省略双引号
我尝试了以下,但代码也替换了引号括起来的单词的逗号
public void Replace_comma_with_tabs(string path)
{
var file = File
.ReadLines(path)
.SkipWhile(line => string.IsNullOrWhiteSpace(line)) // To be on the safe side
.Select((line, index) => line.Replace(',', '\t')) // replace ',' with '\t'
.ToList(); // Materialization, since we write into the same file
File.WriteAllLines(path, file);
}
如何跳过引号括起来的单词的逗号?
【问题讨论】:
-
@CodeCaster 引用字段内的单引号无效。