读书人

StreamReader 获取文本有关问题

发布时间: 2011-12-21 23:56:01 作者: rapoo

StreamReader 获取文本问题
用streamreader读文本的时候能否将 "; " 开头一行的文字整行过滤掉呢?
StreamReader sr = new StreamReader( "post.txt ");
string content = sr.ReadToEnd();
sr.Close();
textBox1.Text = content;

如何改写呢

[解决办法]
StreamReader sr = new StreamReader( "post.txt ");
string content= " ";
string conline;
while(sr.Peek() > = 0)
{
conline=sr.ReadLine();
if(conline.indexOf( "; ")!=0)
content+=conline;
}


sr.Close();
textBox1.Text = content;

[解决办法]
StreamReader sr = new StreamReader( "post.txt ");
StringBuilder sb = new StringBuilder();
string content = sr.ReadLine();
while(content != null)
{
if(!content.StartWith( "; "))
{
sb.Append(content)
}
content = sr.ReadLine();

}
sr.Close();
textBox1.Text = sb.ToString();

读书人网 >C#

热点推荐