|
|
|
@ -7,83 +7,141 @@ namespace Shentun.Utilities |
|
|
|
{ |
|
|
|
public class LanguageConverter |
|
|
|
{ |
|
|
|
public static string GetPYSimpleCode(string data, int len = 50) |
|
|
|
{ |
|
|
|
//public static string GetPYSimpleCode(string data, int len = 50)
|
|
|
|
//{
|
|
|
|
|
|
|
|
string rtnValue = ""; |
|
|
|
// string rtnValue = "";
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(data)) |
|
|
|
{ |
|
|
|
return ""; |
|
|
|
} |
|
|
|
// if (string.IsNullOrWhiteSpace(data))
|
|
|
|
// {
|
|
|
|
// return "";
|
|
|
|
// }
|
|
|
|
|
|
|
|
foreach (char obj in data) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
ChineseChar chineseChar = new ChineseChar(obj); |
|
|
|
string t = chineseChar.Pinyins[0].ToString(); |
|
|
|
rtnValue += t.Substring(0, 1); |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
rtnValue += obj.ToString(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (len > rtnValue.Length) |
|
|
|
{ |
|
|
|
len = rtnValue.Length; |
|
|
|
} |
|
|
|
rtnValue = rtnValue.Substring(0, len).ToUpper(); |
|
|
|
return rtnValue; |
|
|
|
} |
|
|
|
// foreach (char obj in data)
|
|
|
|
// {
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
// ChineseChar chineseChar = new ChineseChar(obj);
|
|
|
|
// string t = chineseChar.Pinyins[0].ToString();
|
|
|
|
// rtnValue += t.Substring(0, 1);
|
|
|
|
// }
|
|
|
|
// catch
|
|
|
|
// {
|
|
|
|
// rtnValue += obj.ToString();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (len > rtnValue.Length)
|
|
|
|
// {
|
|
|
|
// len = rtnValue.Length;
|
|
|
|
// }
|
|
|
|
// rtnValue = rtnValue.Substring(0, len).ToUpper();
|
|
|
|
// return rtnValue;
|
|
|
|
//}
|
|
|
|
|
|
|
|
public static string GetPYCode(string data, int len = 50) |
|
|
|
{ |
|
|
|
//public static string GetPYCode(string data, int len = 50)
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string rtnValue = ""; |
|
|
|
// string rtnValue = "";
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(data)) |
|
|
|
{ |
|
|
|
return ""; |
|
|
|
} |
|
|
|
// if (string.IsNullOrWhiteSpace(data))
|
|
|
|
// {
|
|
|
|
// return "";
|
|
|
|
// }
|
|
|
|
|
|
|
|
foreach (char obj in data) |
|
|
|
// foreach (char obj in data)
|
|
|
|
// {
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
// ChineseChar chineseChar = new ChineseChar(obj);
|
|
|
|
// string t = chineseChar.Pinyins[0].TrimEnd('1', '2', '3', '4', '5').ToUpper();
|
|
|
|
// rtnValue += t;
|
|
|
|
// }
|
|
|
|
// catch
|
|
|
|
// {
|
|
|
|
// rtnValue += obj.ToString();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (len > rtnValue.Length)
|
|
|
|
// {
|
|
|
|
// len = rtnValue.Length;
|
|
|
|
// }
|
|
|
|
// rtnValue = rtnValue.Substring(0, len).ToUpper();
|
|
|
|
// return rtnValue;
|
|
|
|
//}
|
|
|
|
|
|
|
|
//public static string GetPYCodeWithSpace(string data)
|
|
|
|
//{
|
|
|
|
// if(string.IsNullOrWhiteSpace(data))
|
|
|
|
// {
|
|
|
|
// return "";
|
|
|
|
// }
|
|
|
|
// string rtnValue = "";
|
|
|
|
// for (int i = 0; i < data.Length; i++)
|
|
|
|
// {
|
|
|
|
// rtnValue += " " + LanguageConverter.GetPYCode(data.Substring(i, 1));
|
|
|
|
// }
|
|
|
|
// return rtnValue.Trim();
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
#region 新版
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 汉字转首字母
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="strChinese"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static string GetPYSimpleCode(string strChinese) |
|
|
|
{ |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
try |
|
|
|
if (strChinese.Length != 0) |
|
|
|
{ |
|
|
|
ChineseChar chineseChar = new ChineseChar(obj); |
|
|
|
string t = chineseChar.Pinyins[0].TrimEnd('1', '2', '3', '4', '5').ToUpper(); |
|
|
|
rtnValue += t; |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
rtnValue += obj.ToString(); |
|
|
|
StringBuilder fullSpell = new StringBuilder(); |
|
|
|
for (int i = 0; i < strChinese.Length; i++) |
|
|
|
{ |
|
|
|
var chr = strChinese[i]; |
|
|
|
fullSpell.Append(GetSpell(chr)[0]); |
|
|
|
} |
|
|
|
|
|
|
|
return fullSpell.ToString().ToUpper(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (len > rtnValue.Length) |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
len = rtnValue.Length; |
|
|
|
|
|
|
|
Console.WriteLine("首字母转化出错!" + e.Message); |
|
|
|
} |
|
|
|
rtnValue = rtnValue.Substring(0, len).ToUpper(); |
|
|
|
return rtnValue; |
|
|
|
|
|
|
|
return string.Empty; |
|
|
|
} |
|
|
|
|
|
|
|
public static string GetPYCodeWithSpace(string data) |
|
|
|
private static string GetSpell(char chr) |
|
|
|
{ |
|
|
|
if(string.IsNullOrWhiteSpace(data)) |
|
|
|
{ |
|
|
|
return ""; |
|
|
|
} |
|
|
|
string rtnValue = ""; |
|
|
|
for (int i = 0; i < data.Length; i++) |
|
|
|
var coverchr = NPinyin.Pinyin.GetPinyin(chr); |
|
|
|
|
|
|
|
bool isChineses = ChineseChar.IsValidChar(coverchr[0]); |
|
|
|
if (isChineses) |
|
|
|
{ |
|
|
|
rtnValue += " " + LanguageConverter.GetPYCode(data.Substring(i, 1)); |
|
|
|
ChineseChar chineseChar = new ChineseChar(coverchr[0]); |
|
|
|
foreach (string value in chineseChar.Pinyins) |
|
|
|
{ |
|
|
|
if (!string.IsNullOrEmpty(value)) |
|
|
|
{ |
|
|
|
return value.Remove(value.Length - 1, 1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return rtnValue.Trim(); |
|
|
|
|
|
|
|
return coverchr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
} |
|
|
|
} |