C#从字符串读取行

时间:2018-11-25 19:41:16

标签: c#

如何从字符串中读取所有行。

我测试过的代码(不起作用)

string line;
line = mycontent;
StreamReader streamReader = new StreamReader(line);
string data = streamReader.ReadToEnd();
MessageBox.Show(data);

此代码显示错误。 我想创建一个简单的程序,该程序可以从字符串而不是从文件路径读取ReadToEnd。我了解到StreamReader可用于从文件路径读取文件。

我的意图就像我有一个string mycontent = "Simple",我想读到这个mycontent到最后。但是,每次按下按钮,mycontent都会发生变化。 So I want to create something that similar to this code.

谢谢。

PS。错误显示'Illegal characters in path.'

2 个答案:

答案 0 :(得分:2)

StreamReader constructor使用路径作为参数,而不是文字字符串。

改为使用StringReader。要逐行读取,请使用ReadLine()方法。

答案 1 :(得分:0)

使用StringReader()逐行读取字符串:

StringReader reader = new StringReader(multilinestring);
while ((line = reader.ReadLine()) != null)
{
    //do what you want to do with the line;
};