删除“隐形” VB.Net 中的控制字符

发布于 2024-11-14 12:07:36 字数 645 浏览 9 评论 0原文

我当前正在 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 技术交流群。

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

发布评论

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

评论(4

暖阳 2024-11-21 12:07:36

在每行上使用 trim() ,它将删除无关的空格。

Use trim() on each line, it'll remove extraneous whitespace.

灯下孤影 2024-11-21 12:07:36

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.

泼猴你往哪里跑 2024-11-21 12:07:36

如前所述,使用 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.

我最亲爱的 2024-11-21 12:07:36

最近遇到了类似的问题。 Trim() 不起作用,因为在进行分割(或使用 File.ReadAllLines)后,额外的行已经存在。这对我有用:

    Dim allText As String = System.IO.File.ReadAllText(filePath)
    allText = allText.Replace(Chr(13), "")
    Dim lines As String() = allText.Split(vbLf)

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:

    Dim allText As String = System.IO.File.ReadAllText(filePath)
    allText = allText.Replace(Chr(13), "")
    Dim lines As String() = allText.Split(vbLf)

Chr(13) is the Control-M character that result in extra lines using Split() or File.ReadAllLines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文