c# - C#有没有类似于Python的通用换行符的东西

标签 c# newline portability language-features

Python's file objects can now support end of line conventions other than the one followed by the platform on which Python is running. Opening a file with the mode 'U' or 'rU' will open a file for reading in universal newline mode. All three line ending conventions will be translated to a "\n" in the strings returned by the various file methods such as read() and readline().

https://docs.python.org/2.3/whatsnew/node7.html

在 Python 3 中,通用换行符是打开文本文件的默认模式。这意味着,当打开文本文件时,我根本不必关心行结束约定。

我们在 C# 中是否也有这样的便捷功能?

最佳答案

StreamReader.ReadLine这样做:

A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed.

从文件中读取行的便利方法,例如 File.ReadLinesFile.ReadAllLines,使用 StreamReader引擎盖。

我不知道有什么方法可以将文件中的多行作为字符串读取,但是会默默地用规范化的 \n 替换所有行结束字符。 StreamReader.ReadToEnd() 不会以这种方式处理它读取的文本。

关于c# - C#有没有类似于Python的通用换行符的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55664784/

相关文章:

php - 我应该以哪种格式期望提交的文本区域中的换行符

C# decimal.Parse 行为

c - fgets() 在 fscanf() 之后不起作用

c# - 如何将 Click 事件处理程序添加到 Windows 手机上的 TextBlock

c# - 正则表达式匹配任何内容,包括换行符

c - 端口为零的绑定(bind)会将您绑定(bind)到一个自由端口。这是可移植的吗?

c++ - IO流是否可移植

c++ - 确定数字(编码为字符串)是否适合 C++ 中的 64 位整数?

C# "using"语句和 try/catch

c# - LINQ 表达式提取数字的特定数量和值