简繁体转换
[ 2005-07-15 23:49:19 | 作者:
yuhen ]
Strings.StrConv 这个方法还是博客园的一个兄弟发现的,我添了几行代码。做个记录,免得以后忘记。
我的分词组件可以利用这个临时解决一下繁体分词了。
添加 Microsoft Visual Basic.NET Runtime 引用。
using System;
using System.IO;
using System.Text;
using Microsoft.VisualBasic;
namespace Rainsoft.Text
{
/// <summary>
/// 字符串编码转换类
/// </summary>
public class StringEncoding
{
private StringEncoding()
{
}
/// <summary>
/// 将字符串转换为简体中文
/// </summary>
public static string ToSimplifiedChinese(string s)
{
return Microsoft.VisualBasic.Strings.StrConv(s, VbStrConv.SimplifiedChinese, 0);
}
/// <summary>
/// 将文件转换为简体中文
/// </summary>
/// <param name="filename">源文件名</param>
/// <param name="encoding">源文件字符编码</param>
/// <param name="outFilename">目标文件名</param>
/// <param name="outEncoding">目标文件字符编码</param>
/// <example>
/// <code>
/// ToSimplifiedChinese("big5.txt", Encoding.GetEncoding("big5"), "gb.txt", Encoding.GetEncoding("gb2312"));
/// ToSimplifiedChinese("big5.txt", Encoding.GetEncoding("big5"), "gb.txt", Encoding.UTF8);
/// </code>
/// </example>
public static void ToSimplifiedChinese(string filename, Encoding encoding, string outFilename, Encoding outEncoding)
{
StreamReader r = new StreamReader(filename, encoding);
StreamWriter w = new StreamWriter(outFilename, false, outEncoding);
try
{
w.Write(Strings.StrConv(r.ReadToEnd(), VbStrConv.SimplifiedChinese, 0));
w.Flush();
}
finally
{
w.Close();
r.Close();
}
}
/// <summary>
/// 将字符串转换为繁体中文
/// </summary>
public static string ToTraditionalChinese(string s)
{
return Microsoft.VisualBasic.Strings.StrConv(s, VbStrConv.TraditionalChinese, 0);
}
/// <summary>
/// 将文件转换为繁体中文
/// </summary>
/// <param name="filename">源文件名</param>
/// <param name="encoding">源文件字符编码</param>
/// <param name="outFilename">目标文件名</param>
/// <param name="outEncoding">目标文件字符编码</param>
/// <example>
/// <code>
/// ToTraditionalChinese("gb.txt", Encoding.GetEncoding("gb2312"), "gb.txt", Encoding.GetEncoding("big5"));
/// ToTraditionalChinese("gb.txt", Encoding.GetEncoding("gb2312"), "gb.txt", Encoding.UTF8);
/// </code>
/// </example>
public static void ToTraditionalChinese(string filename, Encoding encoding, string outFilename, Encoding outEncoding)
{
StreamReader r = new StreamReader(filename, encoding);
StreamWriter w = new StreamWriter(outFilename, false, outEncoding);
try
{
w.Write(Strings.StrConv(r.ReadToEnd(), VbStrConv.TraditionalChinese, 0));
w.Flush();
}
finally
{
w.Close();
r.Close();
}
}
}
}