姓名序列自动格式化算法

public string Format_WordList(string c_word_list, string c_split = ";", int nEnglish = 0, int deletespace = 0)
    {
        string cFormat = c_word_list.Replace("&nbsp;", " ").Replace("<br />", " ");
        if (nEnglish == 1 && deletespace == 1)
            cFormat = cFormat.Replace(" ", "");//英文姓名序列,先删除空格
        string cOtherCharsSingle = "~`!@#¥%^ &*()-=_+{}[]|:'<,>.?/;";
        if (deletespace == 0)
            cOtherCharsSingle = "~`!@#¥%^&*()-=_+{}[]|:'<,>.?/;";
        string cOtherCharsDouble = ";,【】·、";

        for (int i = 0; i < cOtherCharsSingle.Length; i++)
        {
            string c_1 = cOtherCharsSingle.Substring(i, 1);
            if (c_1 != c_split)
                cFormat = cFormat.Replace(c_1, c_split);
        }
        cFormat = cFormat.Replace("\\", c_split);
        cFormat = cFormat.Replace("\"", c_split);
        //全角
        for (int i = 0; i < cOtherCharsDouble.Length; i++)
            cFormat = cFormat.Replace(cOtherCharsDouble.Substring(i, 1), c_split);
        //分隔符只保留1个
        while (cFormat.IndexOf(c_split + c_split) >= 0)
            cFormat = cFormat.Replace(c_split + c_split, c_split);
        if (cFormat.Length >= 2 && cFormat.Substring(0, 1) == c_split) cFormat = cFormat.Substring(1);
        if (cFormat == c_split) cFormat = "";
        //汉字字符串,删除空格
        if (cFormat.IndexOf(c_split) > 0)
        {
            string[] a_Format = cFormat.Split(new string[] { c_split }, StringSplitOptions.RemoveEmptyEntries);
            string c_new_format = "", c_split_new = "";
            for (int i = 0; i < a_Format.Length; i++)
            {
                string c_1 = a_Format[i];
                if (Check_HZ(c_1, true))
                    c_1 = c_1.Replace(" ", "");
                else
                {
                    //英文格式化:Yatao Ding
                    if (c_1.IndexOf(" ") > 0)
                    {
                        string c_new_e = "", c_split_e = "";
                        string[] a_english = c_1.Split(' ');
                        for (int j = 0; j < a_english.Length; j++)
                        {
                            string c_2 = a_english[j];
                            if (c_2.Length >= 2)
                                c_2 = c_2.Substring(0, 1).ToUpper() + c_2.Substring(1).ToLower();
                            else
                                c_2 = c_2.ToUpper();
                            c_new_e = c_new_e + c_split_e + c_2;
                            c_split_e = " ";
                        }
                        c_1 = c_new_e;
                    }
                    else
                    {
                        if (c_1.Length >= 2)
                            c_1 = c_1.Substring(0, 1).ToUpper() + c_1.Substring(1).ToLower();
                        else
                            c_1 = c_1.ToUpper();
                    }
                }
                c_new_format = c_new_format + c_split_new + c_1;
                c_split_new = c_split;
            }
            cFormat = c_new_format;
        }
        else
        {
            if (Check_HZ(cFormat, true)) cFormat = cFormat.Replace(" ", "");
        }

        return cFormat;
    }
    public bool Check_HZ(string c, bool DeleteSpaceBefore = false)
    {
        bool bReturn = true;
        if (DeleteSpaceBefore) c = c.Replace(" ", "");
        for (int i = 0; i < c.Length; i++)
        {
            if (Regex.IsMatch(c[i].ToString(), @"[\u4e00-\u9fbb]") == false)
            {
                bReturn = false;
                break;
            }
        }
        return bReturn;
    }

算法可以实现自动将汉字、英文姓名统一成相同的格式,例如:

(1)丁亚涛,李有才、顾校华

(2)丁 亚涛,李有 才、顾校 华

自动转换为:丁亚涛;李有才;顾校华

(3)YATAO ding,YOUCAI LI、xiAoHua gu

自动转换为:Yatao Ding;Youcai Li;Xiaohua Gu

如果熟悉正则表达式,代码可以简化为:

public string Format_WordList(string c_word_list, string c_split = ";", int nEnglish = 0)
    {
        string cFormat = c_word_list.Replace("&nbsp;", " ").Replace("<br />", " ").Replace("<br/>", " ").Replace("  ", " ");
        cFormat = cFormat.Replace("\\", c_split).Replace("\"", c_split).Replace("\'", c_split).Replace("\n", c_split).Replace("\r", c_split);
        cFormat = Regex.Replace(cFormat, @"[;,【】·、|]", ";");
        string[] a_format = Regex.Split(cFormat, @"[~`!@#¥%^&*()-=_+{}[]|:'<,>.?/;]");
        for (int i = 0; i < a_format.Length; i++)
        {
            if (Check_HZ(a_format[i], true)) a_format[i] = a_format[i].Replace(" ", "");
            string[] a_sub_format = a_format[i].Split(' ');
            for (int j = 0; j < a_sub_format.Length; j++)
            {
                a_sub_format[j] = Capitalize(a_sub_format[j]);                
            }
            a_format[i] = String.Join(" ", a_sub_format);
        }
        cFormat = String.Join(c_split, a_format.Where(s => !String.IsNullOrEmpty(s)));
        cFormat = Regex.Replace(cFormat, @"[;]+", ";");
        return cFormat;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁亚涛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值