C#:将文本文件内容与字符串变量进行比较

发布于 2024-12-02 08:19:13 字数 470 浏览 1 评论 0原文

我有一个将文本转储到文本文件的应用程序。我认为文本不包含正确的回车符可能存在问题,因此我正在编写一个测试,将该文件的内容与我在代码中声明的字符串变量进行比较。

例如:

1)代码创建一个包含文本的文本文件:

This is line 1
This is line 2
This is line 3

2)我有以下字符串,我想将其与它进行比较:

string testString = "This is line 1\nThis is line 2\nThis is line3"

我知道我可以打开文件流阅读器并逐行读取文本文件并将其存储在可变字符串变量中,同时在每行后面附加“\n”,但想知道这是否是在重新发明轮子(换句话说,.NET 有一个内置的类来处理类似的事情)。提前致谢。

I have an application that dumps text to a text file. I think there might be an issue with the text not containing the proper carriage returns, so I'm in the process of writing a test that will compare the contents of of this file to a string variable that I declare in the code.

Ex:

1) Code creates a text file that contains the text:

This is line 1
This is line 2
This is line 3

2) I have the following string that I want to compare it to:

string testString = "This is line 1\nThis is line 2\nThis is line3"

I understand that I could open a file stream reader and read the text file line by line and store that in a mutable string variable while appending "\n" after each line, but wondering if this is re-inventing the wheel (other words, .NET has a built in class for something like this). Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我不是你的备胎 2024-12-09 08:19:13

您可以使用 StreamReader 的 ReadToEnd() 方法来读取单个字符串中的内容,例如

    using System.IO;

    using(StreamReader streamReader = new StreamReader(filePath))
    {
       string text = streamReader.ReadToEnd();
    }

注意:您必须确保释放资源(上面的代码使用“using”来执行此操作)并且 ReadToEnd() 方法假设流知道何时它已经结束了。对于交互式协议,服务器仅在您请求时发送数据并且不关闭连接,ReadToEnd 可能会无限期地阻塞,因为它没有到达末尾,应该避免,并且您还应该注意字符串中的当前位置应该在开始处。

您还可以使用 ReadAllText,

    // Open the file to read from.
    string readText = File.ReadAllText(path);

这很简单,它打开一个文件,读取所有行并负责关闭。

you can either use StreamReader's ReadToEnd() method to read contents in a single string like

    using System.IO;

    using(StreamReader streamReader = new StreamReader(filePath))
    {
       string text = streamReader.ReadToEnd();
    }

Note: you have to make sure that you release the resources (above code uses "using" to do that) and ReadToEnd() method assumes that stream knows when it has reached an end. For interactive protocols in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely because it does not reach an end, and should be avoided and also you should take care that current position in the string should be at the start.

You can also use ReadAllText like

    // Open the file to read from.
    string readText = File.ReadAllText(path);

which is simple it opens a file, reads all lines and takes care of closing as well.

半﹌身腐败 2024-12-09 08:19:13

不,没有为此内置任何东西。假设您的文件很小,最简单的方法是读取整个文件并进行比较:

var fileContents = File.ReadAllText(fileName);
return testString == filecontents;

如果文件相当长,您可能需要逐行比较文件,因为尽早发现差异将使您能够减少IO。

No, there is nothing built in for this. The easiest way, assuming that your file is small, is to just read the whole thing and compare them:

var fileContents = File.ReadAllText(fileName);
return testString == filecontents;

If the file is fairly long, you may want to compare the file line by line, since finding a difference early on would allow you to reduce IO.

拿命拼未来 2024-12-09 08:19:13

实现读取文件中所有文本的更快方法是
System.IO.File.ReadAllText()

但没有办法缩短字符串级别的比较

A faster way to implement reading all the text in a file is
System.IO.File.ReadAllText()

but theres no way to do the string level comparison shorter

爺獨霸怡葒院 2024-12-09 08:19:13
if(System.IO.File.ReadAllText(filename) == "This is line 1\nThis is line 2\nThis is line3") {
   // it matches
}
if(System.IO.File.ReadAllText(filename) == "This is line 1\nThis is line 2\nThis is line3") {
   // it matches
}
夜空下最亮的亮点 2024-12-09 08:19:13

这应该可行:

StreamReader streamReader = new StreamReader(filePath);
string originalString = streamReader.ReadToEnd();
streamReader.Close();

我认为在 C# 中没有更快的方法。

This should work:

StreamReader streamReader = new StreamReader(filePath);
string originalString = streamReader.ReadToEnd();
streamReader.Close();

I don't think there is a quicker way of doing it in C#.

小红帽 2024-12-09 08:19:13

您可以通过以下方式将整个文件读入字符串变量:

        FileStream stream;
        StreamReader reader;
        stream = new FileStream(yourFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        reader = new StreamReader(stream);
        string stringContainingFilesContent = reader.ReadToEnd();
        // and check for your condition
        if (testString.Equals(stringContainingFilesContent, StringComparison.OrdinalIgnoreCase))

You can read the entire file into a string variable this way:

        FileStream stream;
        StreamReader reader;
        stream = new FileStream(yourFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        reader = new StreamReader(stream);
        string stringContainingFilesContent = reader.ReadToEnd();
        // and check for your condition
        if (testString.Equals(stringContainingFilesContent, StringComparison.OrdinalIgnoreCase))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文