替换文本字符 Shift-JIS [英] Replace text Character Shift-JIS

查看:35
本文介绍了替换文本字符 Shift-JIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Shift Jis 字符有疑问.我使用了以下代码,但它不起作用:

I have a problem with Shift Jis character. I used the following code but it is not working:

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

提前致谢.我希望你能理解我的问题.

Thanks in advance. I hope you understand my problem.

推荐答案

我认为您唯一的问题是您没有存储来自 替换方法.所以我认为你想要的是:

I think your only problem is that you are not storing the return value from the replace-method. So what I think you want is:

line = line.Replace("チ", "Chi")

如果你想把内容替换回文件,你可以这样做:

If you want to replace the content back to the file, you can do it with something like this:

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

这篇关于替换文本字符 Shift-JIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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