获取字符串首字母
Posted hugeboke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取字符串首字母相关的知识,希望对你有一定的参考价值。
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace TravelB2B.Core.Utils.Text 6 7 public class GetInitial 8 9 #region --获取联系人姓名的首字母 10 11 /// <summary> 12 /// 获取联系人姓名的首字母 13 /// </summary> 14 /// <param name="name">姓名</param> 15 /// <returns>姓名的首字母</returns> 16 public static string Get_Name_Frist(string name) 17 18 try 19 20 String _Temp = null; 21 for (int i = 0; i < name.Length; i++) 22 _Temp = _Temp + GetOneIndex(name.Substring(i, 1)); 23 return _Temp.Substring(0, 1).ToUpper(); 24 25 catch 26 return "0"; 27 28 29 //得到单个字符的首字母 30 private static String GetOneIndex(String OneIndexTxt) 31 32 if (Convert.ToChar(OneIndexTxt) >= 0 && Convert.ToChar(OneIndexTxt) < 256) 33 return OneIndexTxt; 34 else 35 36 Encoding gb2312 = Encoding.GetEncoding("gb2312"); 37 byte[] unicodeBytes = Encoding.Unicode.GetBytes(OneIndexTxt); 38 byte[] gb2312Bytes = Encoding.Convert(Encoding.Unicode, gb2312, unicodeBytes); 39 return GetX(Convert.ToInt32( 40 String.Format("0:D2", Convert.ToInt16(gb2312Bytes[0]) - 160) 41 + String.Format("0:D2", Convert.ToInt16(gb2312Bytes[1]) - 160) 42 )); 43 44 45 46 47 48 //根据区位得到首字母 49 50 private static String GetX(int GBCode) 51 52 if (GBCode >= 1601 && GBCode < 1637) return "A"; 53 if (GBCode >= 1637 && GBCode < 1833) return "B"; 54 if (GBCode >= 1833 && GBCode < 2078) return "C"; 55 if (GBCode >= 2078 && GBCode < 2274) return "D"; 56 if (GBCode >= 2274 && GBCode < 2302) return "E"; 57 if (GBCode >= 2302 && GBCode < 2433) return "F"; 58 if (GBCode >= 2433 && GBCode < 2594) return "G"; 59 if (GBCode >= 2594 && GBCode < 2787) return "H"; 60 if (GBCode >= 2787 && GBCode < 3106) return "J"; 61 if (GBCode >= 3106 && GBCode < 3212) return "K"; 62 if (GBCode >= 3212 && GBCode < 3472) return "L"; 63 if (GBCode >= 3472 && GBCode < 3635) return "M"; 64 if (GBCode >= 3635 && GBCode < 3722) return "N"; 65 if (GBCode >= 3722 && GBCode < 3730) return "O"; 66 if (GBCode >= 3730 && GBCode < 3858) return "P"; 67 if (GBCode >= 3858 && GBCode < 4027) return "Q"; 68 if (GBCode >= 4027 && GBCode < 4086) return "R"; 69 if (GBCode >= 4086 && GBCode < 4390) return "S"; 70 if (GBCode >= 4390 && GBCode < 4558) return "T"; 71 if (GBCode >= 4558 && GBCode < 4684) return "W"; 72 if (GBCode >= 4684 && GBCode < 4925) return "X"; 73 if (GBCode >= 4925 && GBCode < 5249) return "Y"; 74 if (GBCode >= 5249 && GBCode <= 5589) return "Z"; 75 if (GBCode >= 5601 && GBCode <= 8794) 76 77 String CodeData = "cjwgnspgcenegypbtwxzdxykygtpjnmjqmbsgzscyjsyyfpggbzgydywjkgaljswkbjqhyjwpdzlsgmr" 78 + "ybywwccgznkydgttngjeyekzydcjnmcylqlypyqbqrpzslwbdgkjfyxjwcltbncxjjjjcxdtqsqzycdxxhgckbphffss" 79 + "pybgmxjbbyglbhlssmzmpjhsojnghdzcdklgjhsgqzhxqgkezzwymcscjnyetxadzpmdssmzjjqjyzcjjfwqjbdzbjgd" 80 + "nzcbwhgxhqkmwfbpbqdtjjzkqhylcgxfptyjyyzpsjlfchmqshgmmxsxjpkdcmbbqbefsjwhwwgckpylqbgldlcctnma" 81 + "eddksjngkcsgxlhzaybdbtsdkdylhgymylcxpycjndqjwxqxfyyfjlejbzrwccqhqcsbzkymgplbmcrqcflnymyqmsqt" 82 + "rbcjthztqfrxchxmcjcjlxqgjmshzkbswxemdlckfsydsglycjjssjnqbjctyhbftdcyjdgwyghqfrxwckqkxebpdjpx" 83 + "jqsrmebwgjlbjslyysmdxlclqkxlhtjrjjmbjhxhwywcbhtrxxglhjhfbmgykldyxzpplggpmtcbbajjzyljtyanjgbj" 84 + "flqgdzyqcaxbkclecjsznslyzhlxlzcghbxzhznytdsbcjkdlzayffydlabbgqszkggldndnyskjshdlxxbcghxyggdj" 85 + "mmzngmmccgwzszxsjbznmlzdthcqydbdllscddnlkjyhjsycjlkohqasdhnhcsgaehdaashtcplcpqybsdmpjlpcjaql" 86 + "cdhjjasprchngjnlhlyyqyhwzpnccgwwmzffjqqqqxxaclbhkdjxdgmmydjxzllsygxgkjrywzwyclzmcsjzldbndcfc" 87 + "xyhlschycjqppqagmnyxpfrkssbjlyxyjjglnscmhcwwmnzjjlhmhchsyppttxrycsxbyhcsmxjsxnbwgpxxtaybgajc" 88 + "xlypdccwqocwkccsbnhcpdyznbcyytyckskybsqkkytqqxfcwchcwkelcqbsqyjqcclmthsywhmktlkjlychwheqjhtj" 89 + "hppqpqscfymmcmgbmhglgsllysdllljpchmjhwljcyhzjxhdxjlhxrswlwzjcbxmhzqxsdzpmgfcsglsdymjshxpjxom" 90 + "yqknmyblrthbcftpmgyxlchlhlzylxgsssscclsldclepbhshxyyfhbmgdfycnjqwlqhjjcywjztejjdhfblqxtqkwhd" 91 + "chqxagtlxljxmsljhdzkzjecxjcjnmbbjcsfywkbjzghysdcpqyrsljpclpwxsdwejbjcbcnaytmgmbapclyqbclzxcb" 92 + "nmsggfnzjjbzsfqyndxhpcqkzczwalsbccjxpozgwkybsgxfcfcdkhjbstlqfsgdslqwzkxtmhsbgzhjcrglyjbpmljs" 93 + "xlcjqqhzmjczydjwbmjklddpmjegxyhylxhlqyqhkycwcjmyhxnatjhyccxzpcqlbzwwwtwbqcmlbmynjcccxbbsnzzl" 94 + "jpljxyztzlgcldcklyrzzgqtgjhhgjljaxfgfjzslcfdqzlclgjdjcsnclljpjqdcclcjxmyzftsxgcgsbrzxjqqcczh" 95 + "gyjdjqqlzxjyldlbcyamcstylbdjbyregklzdzhldszchznwczcllwjqjjjkdgjcolbbzppglghtgzcygezmycnqcycy" 96 + "hbhgxkamtxyxnbskyzzgjzlqjdfcjxdygjqjjpmgwgjjjpkjsbgbmmcjssclpqpdxcdyykypcjddyygywchjrtgcnyql" 97 + "dkljczzgzccjgdyksgpzmdlcphnjafyzdjcnmwescsglbtzcgmsdllyxqsxsbljsbbsgghfjlwpmzjnlyywdqshzxtyy" 98 + "whmcyhywdbxbtlmswyyfsbjcbdxxlhjhfpsxzqhfzmqcztqcxzxrdkdjhnnyzqqfnqdmmgnydxmjgdhcdycbffallztd" 99 + "ltfkmxqzdngeqdbdczjdxbzgsqqddjcmbkxffxmkdmcsychzcmljdjynhprsjmkmpcklgdbqtfzswtfgglyplljzhgjj" 100 + "gypzltcsmcnbtjbhfkdhbyzgkpbbymtdlsxsbnpdkleycjnycdykzddhqgsdzsctarlltkzlgecllkjljjaqnbdggghf" 101 + "jtzqjsecshalqfmmgjnlyjbbtmlycxdcjpldlpcqdhsycbzsckbzmsljflhrbjsnbrgjhxpdgdjybzgdlgcsezgxlblg" 102 + "yxtwmabchecmwyjyzlljjshlgndjlslygkdzpzxjyyzlpcxszfgwyydlyhcljscmbjhblyjlycblydpdqysxktbytdkd" 103 + "xjypcnrjmfdjgklccjbctbjddbblblcdqrppxjcglzcshltoljnmdddlngkaqakgjgyhheznmshrphqqjchgmfprxcjg" 104 + "dychghlyrzqlcngjnzsqdkqjymszswlcfqjqxgbggxmdjwlmcrnfkkfsyyljbmqammmycctbshcptxxzzsmphfshmclm" 105 + "ldjfyqxsdyjdjjzzhqpdszglssjbckbxyqzjsgpsxjzqznqtbdkwxjkhhgflbcsmdldgdzdblzkycqnncsybzbfglzzx" 106 + "swmsccmqnjqsbdqsjtxxmbldxcclzshzcxrqjgjylxzfjphymzqqydfqjjlcznzjcdgzygcdxmzysctlkphtxhtlbjxj" 107 + "lxscdqccbbqjfqzfsltjbtkqbsxjjljchczdbzjdczjccprnlqcgpfczlclcxzdmxmphgsgzgszzqjxlwtjpfsyaslcj" 108 + "btckwcwmytcsjjljcqlwzmalbxyfbpnlschtgjwejjxxglljstgshjqlzfkcgnndszfdeqfhbsaqdgylbxmmygszldyd" 109 + "jmjjrgbjgkgdhgkblgkbdmbylxwcxyttybkmrjjzxqjbhlmhmjjzmqasldcyxyqdlqcafywyxqhz"; 110 String _gbcode = GBCode.ToString(); 111 int pos = (Convert.ToInt16(_gbcode.Substring(0, 2)) - 56) * 94 + Convert.ToInt16(_gbcode.Substring(_gbcode.Length - 2, 2)); 112 return CodeData.Substring(pos - 1, 1); 113 114 return "0"; 115 116 117 118 119 #endregion 120 121 122
以上是关于获取字符串首字母的主要内容,如果未能解决你的问题,请参考以下文章