为什么我在F#中的StreamReader.ReadLine()不能将\ n读为新行? [英] Why does my StreamReader.ReadLine () in F# not read \n as a new line?

查看:209
本文介绍了为什么我在F#中的StreamReader.ReadLine()不能将\ n读为新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试以此方式读取文件:

I am currently trying to read a file as this:

53**7****\n6**195***\n*98****6*\n8***6***3\n4**8*3**1\n7***2***6\n*6****28*\n***419**5\n****8**79\n

并将其写入屏幕,但用新行代替/n.

And write it into the screen, but with new lines instead of the /n.

在方法StreamReader.ReadLine()的msdn描述中,它表示: 行定义为一系列字符,后跟换行符("\ n"),回车符("\ r")或回车符后紧跟换行符("\ r \ n").返回的字符串不包含回车符或换行符.如果到达输入流的末尾,则返回的值为null.

On the msdn description of the method StreamReader.ReadLine () it says that: 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. The returned value is null if the end of the input stream is reached.

为什么我的程序不能将\n解释为换行符?

Why does my program not interpret \n as a new line?

推荐答案

好吧,问题在于ReadLine的文档讨论的是'\n'(单个)字符,而实际上您有两个"\\n" -字符字符串.

Well, the problem is that the documentation for ReadLine is talking about the '\n' (single) character, while you actually have a "\\n" two-character string.

在C#中,\用作转义字符-例如,\n表示ASCII值为10的字符.但是,文件不会根据C#规则进行 解析(即好东西!).由于您的文件没有立即数10个字符,因此不会将它们解释为结尾,正确的是-ASCII的立即数转换为(92,110).

In C#, \ is used as an escape character - for example, \n represents the character with ASCII value of 10. However, files are not parsed according to C# rules (that's a good thing!). Since your file doesn't have the literal 10-characters, they aren't interpreted as endlines, and rightly so - the literal translation in ASCII would be (92, 110).

只需使用Split(或Replace),就可以了.基本上,您想将"\\n"替换为"\n"(或更佳的是Environment.NewLine).

Just use Split (or Replace), and you'll be fine. Basically, you want to replace "\\n" with "\n" (or better, Environment.NewLine).

这篇关于为什么我在F#中的StreamReader.ReadLine()不能将\ n读为新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆