检查字符串在C#中是半角还是全角 [英] Check if a string is half width or full width in C#

查看:232
本文介绍了检查字符串在C#中是半角还是全角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#应用程序位于日语Windows操作系统-以全角字符显示拉丁语

我在上面的链接中提到了可接受的答案,并且正在使用下面的代码将日语字符串从全角转换为半角,但它返回的是相同的全角字符串而没有进行转换.

I referred the accepted answer in the above link and is using the code below to convert Japanese string from full width to half width but it is returning the same full width string without converting.

string userInput = "チヨチヨチチヨチヨチ";
string result = userInput.Normalize(NormalizationForm.FormKC);

预期输出的一半宽度:チ ヨ チ ヨ チ チ ヨ チ ヨ チ 实际输出:チヨチヨチチヨチヨチ(全宽)

Expected output in half width: チヨチヨチチヨチヨチ Actual output: チヨチヨチチヨチヨチ (full width)

但是,即使上面的代码应该将全角字符串转换为半角,当我将半角字符串(チ ヨ チ ヨ チ チ ヨ チ ヨ チ)传递给上述代码时,它也会将其转换为全角形式(チヨチヨチチヨチヨチ).

However, even though the above code is supposed to convert a full width string to half width, when I pass the half width string (チヨチヨチチヨチヨチ) to the above code, it converts it to full width form (チヨチヨチチヨチヨチ).

我在这里做错了什么?

What am I doing wrong here?

无论如何,如果我的字符串已经是半角,我不希望执行以上代码.

Anyways I don’t want the above code to be executed if my string is already in half-width.

如何检查字符串是半角还是全角?

How can I check if a string is half width or full width?

推荐答案

根据文档,规范化方法按预期工作.必须将字符转换为标准字符,以便可以正确应用二进制比较.

According to this document, the normalize method works as expected. It must convert characters to the standard characters, so the binary comparison can be applied correctly.

但是,如果您希望始终将全角转换为半角的自定义转换,则可以创建此链接可能有助于创建此地图.

But if you want a custom conversion that always converts full-width to half-width, you can create a Dictionary to map full-width to half-width characters. This link may be helpful to create this map.

如果要确保字符串为半角,则如果它包含任何全角字符,则将其拒绝.创建所有全角字符(拉丁和日语)的字符串,然后在全角字符串中找到要测试的字符串的所有字符.

If you want to be sure that the string is in half-width then if it contains any full-width character, it is rejected. Create a string of all full-width characters(Latin and Japanese) then find all characters of the to test string in the full-width characters string.

我为此编写了isHalfWidthString方法,并且还将全角转换为半角转换法.我认为这可能会有所帮助:

I wrote isHalfWidthString method for this purpose and added full-width to half-width converter method also. I thought it may be helpful:

    public class FullWidthCharactersHandler
    {
        static Dictionary<char, char> fullWidth2halfWidthDic;
        static FullWidthCharactersHandler()
        {
            fullWidth2halfWidthDic = new Dictionary<char, char>();
            string fullWidthChars = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンッァィゥェォャュョ゙゚ー0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            string halfWidthChars = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンッァィゥェォャュョ゙゚ー0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            for (int i = 0; i < fullWidthChars.Length; i++)
            {
                fullWidth2halfWidthDic.Add(fullWidthChars[i], halfWidthChars[i]);
            }
        }

        public static bool isHalfWidthString(string toTestString)
        {
            bool isHalfWidth = true;
            foreach (char ch in toTestString)
            {
                if (fullWidth2halfWidthDic.ContainsKey(ch))
                {
                    isHalfWidth = false;
                    break;
                }
            }
            return isHalfWidth;
        }

        public static string convertFullWidthToHalfWidth(string theString)
        {
            StringBuilder sbResult = new StringBuilder(theString);
            for (int i = 0; i < theString.Length; i++)
            {
                if (fullWidth2halfWidthDic.ContainsKey(theString[i]))
                {
                    sbResult[i] = fullWidth2halfWidthDic[theString[i]];
                }
            }
            return sbResult.ToString();
        }
    }

要进行测试,请使用此链接.

我更新了代码,以使用Dictionary以获得更好的性能.

I updated the code to use Dictionary for better performance.

这篇关于检查字符串在C#中是半角还是全角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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