我正在编写一个重写文本文件的程序 - 最初我只需要删除任何一行"#"在它,工作得很好 - 现在我需要删除哈希,但保持在哈希之间的字符串。所以例如" ##### textHere #####我只想要textHere字符串......
这是我的代码:
string lineData = "#";
string copy = "rcpy";
string dash ="-------";
string total = "total";
string vv = "vvcp";
int count = 0;
string Name = "Name";
// Read the file and display it line by line.
using (System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text))
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"C:\test4.txt"))
{
//while the reader reads the lines, perform the re-write based on these conditions
while ((line = file.ReadLine()) != null)
{
line = line.Trim();
if (line.Contains(Name))
{
count++;
}
if (line.Contains(Name) && count >= 2 || line.Contains(copy) || line.Contains(dash) || line.Contains(total) || line.Contains(vv)) || line.Contains(lineData))
{
//then right to new file
}
else
{
string[] keys = new string[] { "R1", "R5" };
string result = keys.FirstOrDefault<string>(s=>line.Contains(s));
switch(result)
{
case "R1":
writer.WriteLine("R5" + "\t" + line);
break;
case "R5":
writer.WriteLine("R1" + "\t" + line);
break;
default:
writer.WriteLine("\t" + line);
break;
}
//then right to new file
}
}
}
MessageBox.Show("Formatting Complete");
}
}
}
答案 0 :(得分:2)
yourString.Replace("#", String.Empty);
答案 1 :(得分:1)
您可以使用下面提到的代码
if(line.Contains(lineData))
{
line=line.Replace("#","");
}