简体   繁体   English

C# 文本文件到字符串数组以及如何删除特定字符串?

[英]C# text file to string array and how to remove specific strings?

I need read a text file (10mb) and convert to .csv.我需要读取一个文本文件 (10mb) 并转换为 .csv。 See below portion of code:请参阅以下代码部分:

string DirPathForm = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);'
string[] lines = File.ReadAllLines(DirPathForm + @"\file.txt");

Some portion of the text file have a pattern.文本文件的某些部分具有模式。 So, used as below:所以,使用如下:

string[] lines1 = lines.Select(x => x.Replace("abc[", "ab,")).ToArray();
Array.Clear(lines, 0, lines.Length);
lines = lines1.Select(x => x.Replace("] CDE  ", ",")).ToArray();

Some portion does not have a pattern to use directly Replace.有些部分没有模式可以直接替换。 The question is how remove the characters, numbers and whitespaces in this portion.问题是如何删除这部分中的字符、数字和空格。 Please see below?请看下面?

string[] lines = {
    "a]  773  b",
    "e] 1597  t",
    "z]    0  c"
};

to get the result below:得到以下结果:

string[] result = {
    "a,b",
    "e,t",
    "z,c"
};

obs: the items removed need be replaced by ",". obs:删除的项目需要用“,”替换。

First of all, you should not use ReadAllLines since it is a huge file operation.首先,您不应该使用 ReadAllLines,因为它是一个巨大的文件操作。 It will load all the data into RAM and it is not correct.它会将所有数据加载到 RAM 中,这是不正确的。 Instead, read the lines one by one in a loop.相反,请在循环中逐行阅读。

Secondly, you can definitely use regex to replace data from the first condition to the second one.其次,您绝对可以使用正则表达式将数据从第一个条件替换为第二个条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM