删除“隐形” VB.Net 中的控制字符
我当前正在 VB.Net 中使用“
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(file)
文件包含多行文本”读取文本文件,当我读取文本文件时,它知道它们位于不同的行上并相应地打印出来。
但是,当我尝试将 fileReader 拆分为不同行的数组时,即使我使用 Split(ControlChars.Cr)
或 Split(ControlChars.NewLine )
。它会成功地将其分割成单独的行,但是当我显示它时,它会将文本“推”下一行,就像换行符仍然存在一样...
有谁对发生的事情以及我如何做有任何想法删除这些“不可见”的控制字符。
文本文件:
测试1
测试2
测试3
测试4
文件读取器:
测试1
测试2
测试3
Test4
lines() 打印输出
Test1
测试2
测试3
测试4
I am currently reading in a text file in VB.Net using
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(file)
File contains several lines of text and when I read in the text file, it knows that they are on separate lines and prints them out accordingly.
However, when I try to split fileReader into an array of the different lines, the line break seems to stay there, even if I use Split(ControlChars.Cr)
or Split(ControlChars.NewLine)
. It will successfully split it into the separate lines but when I display it, it will "push" the text down a line, like the line break is still there...
Does anyone have any ideas on what is going on and how I can remove these "invisible" control chars.
Text File:
Test1
Test2
Test3
Test4
fileReader:
Test1
Test2
Test3
Test4
lines() printout
Test1
Test2
Test3
Test4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在每行上使用
trim()
,它将删除无关的空格。Use
trim()
on each line, it'll remove extraneous whitespace.System.IO.File 类有一个 ReadAllLines 方法,它实际上会返回一个字符串数组,每行一个。
如果该方法也不起作用,我将准确检查哪些字节导致了问题。在监视窗口中,您可以执行 System.Text.Encoding.ASCII.GetBytes (sampleLine) 并准确检查您正在使用的内容。
我假设您正在使用 ASCII 编码,如果没有,您需要使用正确的选项替换 ASCII,然后将文件读取修改为基于该编码读取。
The System.IO.File class has a ReadAllLines method that will actually give you back an array of strings, one per line.
If that method doesn't work, either, I would examine exactly what bytes are causing you issues. In the watch window, you can do a System.Text.Encoding.ASCII.GetBytes (sampleLine) and examine exactly what you are working with.
I'm assuming you are using ASCII encoding, if not, you'll need to swap out ASCII with the correct option, and then modify your file read to read based on that encoding, as well.
如前所述,使用 Readalllines 方法将其自动拆分。
您遇到的问题是 PC ASCII 文件通常用回车符和换行符分割,仅分割一个文件就会留下另一个文件。您可以按照上述方式进行拆分和修剪,或者使用按字符串而不是字符进行拆分的其他拆分。
dim s() as string = Split(fileReader ,vbCrLf)
Trim 也会从数据中删除空格,具体取决于您可能遇到问题的情况。
As mentioned use the Readalllines method to have it split automatically.
The problem you are having is PC ASCII files are usually split with a carriage return and a new line, splitting on just one will leave the other. You can split and trim as mentioned or use the other split that splits on strings instead of chars.
dim s() as string = Split(fileReader ,vbCrLf)
Trim will remove spaces from the data as well, depending on your situation that could be a problem for you.
最近遇到了类似的问题。 Trim() 不起作用,因为在进行分割(或使用 File.ReadAllLines)后,额外的行已经存在。这对我有用:
Chr(13) 是 Control-M 字符,它会使用 Split() 或 File.ReadAllLines 产生额外的行。
Ran into a similar problem recently. The Trim() doesnt work because the extra lines are already there after doing the split (or using File.ReadAllLines). Here's what worked for me:
Chr(13) is the Control-M character that result in extra lines using Split() or File.ReadAllLines.