最近悟出來一個道理,在這兒分享給大家:學歷代表你的過去,能力代表你的現在,學習代表你的將來。
十年河東十年河西,莫欺少年窮
學無止境,精益求精
本節探討C#獲取漢字拼音首字母的方法:
代碼類東西,直接上代碼:
/// <summary>
/// 在指定的字符串列表CnStr中檢索符合拼音索引字符串
/// </summary>
/// <param name="CnStr">漢字字符串</param>
/// <returns>相對應的漢語拼音首字母串</returns>
public static string GetSpellCode(string CnStr)
{
string strTemp = "";
int iLen = CnStr.Length;
int i = 0;
for (i = 0; i <= iLen - 1; i++)
{
strTemp += GetCharSpellCode(CnStr.Substring(i, 1));
break;
}
return strTemp;
}
/// <summary>
/// 得到一個漢字的拼音第一個字母,如果是一個英文字母則直接返回大寫字母
/// </summary>
/// <param name="CnChar">單個漢字</param>
/// <returns>單個大寫字母</returns>
private static string GetCharSpellCode(string CnChar)
{
long iCnChar;
byte[] ZW = System.Text.Encoding.Default.GetBytes(CnChar);
//如果是字母,則直接返回首字母
if (ZW.Length == 1)
{
return CommonMethod.CutString(CnChar.ToUpper(),1);
}
else
{
// get the array of byte from the single char
int i1 = (short)(ZW[0]);
int i2 = (short)(ZW[1]);
iCnChar = i1 * 256 + i2;
}
// iCnChar match the constant
if ((iCnChar >= 45217) && (iCnChar <= 45252))
{
return "A";
}
else if ((iCnChar >= 45253) && (iCnChar <= 45760))
{
return "B";
}
else if ((iCnChar >= 45761) && (iCnChar <= 46317))
{
return "C";
}
else if ((iCnChar >= 46318) && (iCnChar <= 46825))
{
return "D";
}
else if ((iCnChar >= 46826) && (iCnChar <= 47009))
{
return "E";
}
else if ((iCnChar >= 47010) && (iCnChar <= 47296))
{
return "F";
}
else if ((iCnChar >= 47297) && (iCnChar <= 47613))
{
return "G";
}
else if ((iCnChar >= 47614) && (iCnChar <= 48118))
{
return "H";
}
else if ((iCnChar >= 48119) && (iCnChar <= 49061))
{
return "J";
}
else if ((iCnChar >= 49062) && (iCnChar <= 49323))
{
return "K";
}
else if ((iCnChar >= 49324) && (iCnChar <= 49895))
{
return "L";
}
else if ((iCnChar >= 49896) && (iCnChar <= 50370))
{
return "M";
}
else if ((iCnChar >= 50371) && (iCnChar <= 50613))
{
return "N";
}
else if ((iCnChar >= 50614) && (iCnChar <= 50621))
{
return "O";
}
else if ((iCnChar >= 50622) && (iCnChar <= 50905))
{
return "P";
}
else if ((iCnChar >= 50906) && (iCnChar <= 51386))
{
return "Q";
}
else if ((iCnChar >= 51387) && (iCnChar <= 51445))
{
return "R";
}
else if ((iCnChar >= 51446) && (iCnChar <= 52217))
{
return "S";
}
else if ((iCnChar >= 52218) && (iCnChar <= 52697))
{
return "T";
}
else if ((iCnChar >= 52698) && (iCnChar <= 52979))
{
return "W";
}
else if ((iCnChar >= 52980) && (iCnChar <= 53640))
{
return "X";
}
else if ((iCnChar >= 53689) && (iCnChar <= 54480))
{
return "Y";
}
else if ((iCnChar >= 54481) && (iCnChar <= 55289))
{
return "Z";
}
else
return ("?");
}
截取字符串的方法:
#region 截取字符長度 static string CutString(string str, int len)
/// <summary>
/// 截取字符長度
/// </summary>
/// <param name="str">被截取的字符串</param>
/// <param name="len">所截取的長度</param>
/// <returns>子字符串</returns>
public static string CutString(string str, int len)
{
if (str == null || str.Length == 0 || len <= 0)
{
return string.Empty;
}
int l = str.Length;
#region 計算長度
int clen = 0;
while (clen < len && clen < l)
{
//每遇到一個中文,則將目標長度減一。
if ((int)str[clen] > 128) { len--; }
clen++;
}
#endregion
if (clen < l)
{
return str.Substring(0, clen) + "...";
}
else
{
return str;
}
}
/// <summary>
/// //截取字符串中文 字母
/// </summary>
/// <param name="content">源字符串</param>
/// <param name="length">截取長度!</param>
/// <returns></returns>
public static string SubTrueString(object content, int length)
{
string strContent = NoHTML(content.ToString());
bool isConvert = false;
int splitLength = 0;
int currLength = 0;
int code = 0;
int chfrom = Convert.ToInt32("4e00", 16); //范圍(0x4e00~0x9fff)轉換成int(chfrom~chend)
int chend = Convert.ToInt32("9fff", 16);
for (int i = 0; i < strContent.Length; i++)
{
code = Char.ConvertToUtf32(strContent, i);
if (code >= chfrom && code <= chend)
{
currLength += 2; //中文
}
else
{
currLength += 1;//非中文
}
splitLength = i + 1;
if (currLength >= length)
{
isConvert = true;
break;
}
}
if (isConvert)
{
return strContent.Substring(0, splitLength);
}
else
{
return strContent;
}
}
public static int GetStringLenth(object content)
{
string strContent = NoHTML(content.ToString());
int currLength = 0;
int code = 0;
int chfrom = Convert.ToInt32("4e00", 16); //范圍(0x4e00~0x9fff)轉換成int(chfrom~chend)
int chend = Convert.ToInt32("9fff", 16);
for (int i = 0; i < strContent.Length; i++)
{
code = Char.ConvertToUtf32(strContent, i);
if (code >= chfrom && code <= chend)
{
currLength += 2; //中文
}
else
{
currLength += 1;//非中文
}
}
return currLength;
}
#endregion
以上便是完整代碼,謝謝!
在此,順便說下數據庫按照漢字首字母進行排序的方法:
oracle :
在oracle9i中新增了按照拼音、部首、筆畫排序功能。設置NLS_SORT值
SCHINESE_RADICAL_M 按照部首(第一順序)、筆劃(第二順序)排序
SCHINESE_STROKE_M 按照筆劃(第一順序)、部首(第二順序)排序
SCHINESE_PINYIN_M 按照拼音排序,系統的默認排序方式為拼音排序
舉例如下:
表名為 dept ,其中name字段是中文,下面分別實現按照單位名稱的筆劃、部首和拼音排序。
//按照筆劃排序
select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_STROKE_M');
//按照部首排序
select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_RADICAL_M');
//按照拼音排序,此為系統的默認排序方式
select * from dept order by nlssort(name,'NLS_SORT=SCHINESE_PINYIN_M');
sqlserver
select * from table order by name collate Chinese_PRC_CS_AS_KS_WS
執行結果:select * from Table where cateFid=0 order by cateName collate Chinese_PRC_CS_AS_KS_WS
@陳臥龍的博客