菜鸟问题,如何快速删除文本文件中的空白行?
现在有一个文本文件,里面的内容如下,中间有很多空白行
年份
2011/12
2010/12
2009/12
2008/12
2007/12
盈利(百万)
46,055
26,836
19,618
13,029
27,678
盈利增长(%)
71.62
36.79
50.57
-52.93
53.13
现在想转成这样:
年份
2011/12
2010/12
2009/12
2008/12
2007/12
盈利(百万)
46,055
26,836
19,618
13,029
27,678
盈利增长(%)
71.62
36.79
50.57
-52.93
53.13
也就是去除掉中间的空白行,有什么简单易行的办法吗?
[解决办法]
- C# code
Regex.Replace(yourText,@"\s+","\r\n");
[解决办法]
- C# code
System.IO.FileStream streamTxt = new System.IO.FileStream(AppDomain.CurrentDomain.BaseDirectory+"test.txt", System.IO.FileMode.Open);byte[] bytesTxt = new byte[streamTxt.Length];streamTxt.Read(bytesTxt, 0, bytesTxt.Length);string strTxt = System.Text.Encoding.Default.GetString(bytesTxt);strTxt=System.Text.RegularExpressions.Regex.Replace(strTxt, @"\s+", "\r\n");streamTxt.SetLength(0);streamTxt.Write(System.Text.Encoding.Default.GetBytes(strTxt), 0, System.Text.Encoding.Default.GetBytes(strTxt).Length);streamTxt.Close();