简体   繁体   English

在z#中将zenkaku字符转换为hankaku,反之亦然

[英]Converting zenkaku characters to hankaku and vice-versa in C#

As it says in the header line, I want to convert zenkaku characters to hankaku ones and vice-vrsa in C#, but can't figure out how to do it. 正如它在标题行中所说,我想将zenkaku字符转换为hankaku字符和C#中的副vrsa,但无法弄清楚如何做到这一点。 So, say "ラーメン" to "ラーメン" and the other way around. 所以,说“ラーメン”到“ラーメン”,反之亦然。 Would it be possible to write this in a method which determines automatically which way the conversion needs to go, based on the format of the input? 是否有可能在一种方法中编写它,根据输入的格式自动确定转换需要的方式?

You can use the Strings.StrConv() method by including a reference to Microsoft.VisualBasic.dll, or you can p/invoke the LCMapString() native function: 您可以通过包含对Microsoft.VisualBasic.dll的引用来使用Strings.StrConv()方法,或者您可以p /调用LCMapString()本机函数:

private const uint LOCALE_SYSTEM_DEFAULT = 0x0800;
private const uint LCMAP_HALFWIDTH = 0x00400000;

public static string ToHalfWidth(string fullWidth)
{
    StringBuilder sb = new StringBuilder(256);
    LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_HALFWIDTH, fullWidth, -1, sb, sb.Capacity);
    return sb.ToString();
}

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern int LCMapString(uint Locale, uint dwMapFlags, string lpSrcStr, int cchSrc, StringBuilder lpDestStr, int cchDest);

and you can also do the reverse: 你也可以这样做:

private const uint LCMAP_FULLWIDTH = 0x00800000;

public static string ToFullWidth(string halfWidth)
{
    StringBuilder sb = new StringBuilder(256);
    LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_FULLWIDTH, halfWidth, -1, sb, sb.Capacity);
    return sb.ToString();
}

As for detecting the format of the input string, I'm not aware of an easy way without doing a conversion first and comparing results. 至于检测输入字符串的格式,我不知道没有先进行转换并比较结果的简单方法。 (What if the string contains both full-width and half-width characters?) (如果字符串包含全角和半角字符,该怎么办?)

One approach is to compile a list of all characters you want to convert and how they map to each other, and then iterate the input string and replace all characters in the list with their equivalent. 一种方法是编译要转换的所有字符的列表以及它们如何相互映射,然后迭代输入字符串并用等效字符替换列表中的所有字符。

var fullToHalf = new Dictionary<char, char>
{
    ...
    { '\u30E9', '\uFF97' }, // KATAKANA LETTER RA -> HALFWIDTH KATAKANA LETTER RA
    { '\u30EA', '\uFF98' }, // KATAKANA LETTER RI -> HALFWIDTH KATAKANA LETTER RI
    ...
};

var halfToFull = fullToHalf.ToDictionary(kv => kv.Value, kv => kv.Key);

var input = "\u30E9";

var isFullWidth = input.All(ch => fullToHalf.ContainsKey(ch));
var isHalfWidth = input.All(ch => halfToFull.ContainsKey(ch));

var result = new string(input.Select(ch => fullToHalf[ch]).ToArray());
// result == "\uFF97"

Unicode Chart: Halfwidth and Fullwidth Forms (FF00-FFEF) Unicode图表:半宽和全宽格式(FF00-FFEF)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM