从行中删除散列,但保留字符串

本文关键字:保留 保留字 字符串 删除 | 更新日期: 2023-09-27 18:06:07

我正在编写一个重写文本文件的程序-最初我只需要删除其中带有"#"的任何行,这工作得很好-现在我需要删除哈希,但保留哈希之间的字符串。例如"#####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");
            }
        }
    }

从行中删除散列,但保留字符串

yourString.Replace("#", String.Empty);

可以使用下面提到的代码

if(line.Contains(lineData))
{
 line=line.Replace("#","");
}