public string Format_WordList(string c_word_list, string c_split = ";", int nEnglish = 0, int deletespace = 0)
{
string cFormat = c_word_list.Replace(" ", " ").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(" ", " ").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;
}