我正在阅读 VB.net 代码中的文本文件。我想阅读这一行,其中包含一些特殊字符,其 dec 值为 176,十六进制值为 B1,符号为 + 和 - 在底部。下面是显示该值的 URL:
http://www.ascii.cl/htmlcodes.htm
如何在 VB.net 中读取值为 176 的特殊字符
线是这样的
"2314 Test1 Test2 Test3 12.3 special character that has a Dec value of 176 1234"
每个单词由制表符分隔。
这是我目前所做的:
reader = File.OpenText(fileInfo.FullName)
While reader.Peek <> -1
line = reader.ReadLine()
End While
reader.Close()
reader.Dispose()
我也试过这样做:
System.Text.Encoding.GetEncoding(line)
我们将不胜感激。
最佳答案
使用 StreamReader
类的 this constructor(来自 System.IO
命名空间):
Using reader As StreamReader = New StreamReader("C:\Users\trashr0x\Desktop\test.txt", System.Text.Encoding.Unicode)
Dim line As String = reader.ReadLine()
End Using
您还可以使用 File.ReadAllText
方法(同样来自 System.IO
):
Dim testString as String = File.ReadAllText("C:\Users\trashr0x\Desktop\test.txt", System.Text.Encoding.Unicode)
我已将编码设置为 Encoding.Unicode
。如果您不知道文件的编码,可以尝试其他值:
Encoding.ASCII
(正如@Jon Skeet 在您的问题评论中指出的,此符号不是 ASCII)编码.UTF32
编码.UTF8
编码.UTF7
编码.BigEndianUnicode
Encoding.Default
(默认编码为ANSI)
旁白一个有趣的读物:Joel Spolsky 的 "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)"。
关于c# - VB.net 在读取文本文件时读取 ASCII 码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35024506/