Obtain the first letter C # Of a Chinese character string.

Source: Internet
Author: User

Obtain the first letter of the Chinese character in the string (modified according to http://blog.csdn.net/iwebsms/archive/2004/09/28/119851.aspx)



The code may be bloated. I hope some friends can help me correct it.



// Returns the first letter of a given string.
Private String IndexCode (String IndexTxt)
{
String _ Temp = null;
For (int I = 0; I <IndexTxt. Length; I ++)
_ Temp = _ Temp + GetOneIndex (IndexTxt. Substring (I, 1 ));
Return _ Temp;
}



// Obtain the first letter of a single character
Private String GetOneIndex (String OneIndexTxt)
{
If (Convert. ToChar (OneIndexTxt)> = 0 & Convert. ToChar (OneIndexTxt) <256)
Return OneIndexTxt;
Else
{
Encoding gb2312 = Encoding. GetEncoding ("gb2312 ");
Byte [] unicodeBytes = Encoding. Unicode. GetBytes (OneIndexTxt );
Byte [] gb2312Bytes = Encoding. Convert (Encoding. Unicode, gb2312, unicodeBytes );
Return GetX (Convert. ToInt32 (
String. Format ("{0: D2}", Convert. ToInt16 (gb2312Bytes [0])-160)
+ String. Format ("{0: D2}", Convert. ToInt16 (gb2312Bytes [1])-160)
));
}

}


// Obtain the first letter Based on the location
Private String GetX (int GBCode)
{
If (GBCode> = 1601 & GBCode <1637) return "";
If (GBCode> = 1637 & GBCode <1833) return "B ";
If (GBCode> = 1833 & GBCode <2078) return "C ";
If (GBCode> = 2078 & GBCode <2274) return "D ";
If (GBCode> = 2274 & GBCode <2302) return "E ";
If (GBCode> = 2302 & GBCode <2433) return "F ";
If (GBCode> = 2433 & GBCode <2594) return "G ";
If (GBCode> = 2594 & GBCode <2787) return "H ";
If (GBCode> = 2787 & GBCode <3106) return "J ";
If (GBCode> = 3106 & GBCode <3212) return "K ";
If (GBCode> = 3212 & GBCode <3472) return "L ";
If (GBCode> = 3472 & GBCode <3635) return "M ";
If (GBCode> = 3635 & GBCode <3722) return "N ";
If (GBCode> = 3722 & GBCode <3730) return "O ";
If (GBCode> = 3730 & GBCode <3858) return "P ";
If (GBCode> = 3858 & GBCode <4027) return "Q ";
If (GBCode> = 4027 & GBCode <4086) return "R ";
If (GBCode> = 4086 & GBCode <4390) return "S ";
If (GBCode> = 4390 & GBCode <4558) return "T ";
If (GBCode> = 4558 & GBCode <4684) return "W ";
If (GBCode> = 4684 & GBCode <4925) return "X ";
If (GBCode> = 4925 & GBCode <5249) return "Y ";
If (GBCode> = 5249 & GBCode <= 5589) return "Z ";
If (GBCode> = 5601 & GBCode <= 8794)
{
String CodeData = "success"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Alarm"
+ "Success ";
String _ gbcode = GBCode. ToString ();
Int pos = (Convert. toInt16 (_ gbcode. substring (0, 2)-56) * 94 + Convert. toInt16 (_ gbcode. substring (_ gbcode. length-2, 2 ));
Return CodeData. Substring (pos-1, 1 );
}
Return "";
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.