Shift Jis字符有问题。我使用了以下代码,但它不起作用:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim line As String
Using sr As StreamReader = New StreamReader("C:\2\File.txt", System.Text.Encoding.GetEncoding(932))
line = sr.ReadToEnd
line.Replace("チ", "Chi")
End Using
End Sub
End Class
提前致谢。我希望你能理解我的问题。
答案 0 :(得分:0)
我认为您唯一的问题是您没有存储replace-method的返回值。所以我认为你想要的是:
line = line.Replace("チ", "Chi")
如果要将内容替换回文件,可以使用以下内容执行此操作:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim filename As String = "C:\2\File.txt"
Dim line As String
Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(932)
' You could replace the using-block with:
' line = File.ReadAllText(filename, encoding)
Using sr As StreamReader = New StreamReader(filename, encoding)
line = sr.ReadToEnd
line.Replace("チ", "Chi")
End Using
File.WriteAllText(filename, line, encoding)
End Sub