Obtain the first letter of Chinese pinyin (C #) based on the location #)

Source: Internet
Author: User
Using system;
Using system. text;

Namespace code
{
/// <Summary>
/// Summary of searchcode.
/// </Summary>
Public class searchcode
{
Public searchcode ()
{
//
// Todo: add the constructor logic here
//
}
/// <Summary>
/// Returns the first letter of a given string.
/// </Summary>
/// <Param name = "indextxt"> string </param>
/// <Returns> </returns>
Public String indexcode (string indextxt)
{
String _ temp = NULL;
For (INT I = 0; I <indextxt. length; I ++)
_ Temp = _ temp + getoneindex (indextxt. substring (I, 1 ));
Return _ temp;
}

/// <Summary>
/// Returns the first letter of the given string
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> </returns>
Public String oneindexcode (string Str)
{
Return getoneindex (Str. substring (0, 1 ));
}

// Obtain the first letter of a single character
Private string getoneindex (string oneindextxt)
{
If (convert. tochar (oneindextxt)> = 0 & convert. tochar (oneindextxt) <256)
Return oneindextxt. toupper ();
Else
{
Encoding gb2312 = encoding. getencoding ("gb2312 ");
Byte [] unicodebytes = encoding. Unicode. getbytes (oneindextxt );
Byte [] gb2312bytes = encoding. Convert (encoding. Unicode, gb2312, unicodebytes );
Try
{
Return getx (convert. toint32 (
String. Format ("{0: D2}", convert. toint16 (gb2312bytes [0])-160)
+ String. Format ("{0: D2}", convert. toint16 (gb2312bytes [1])-160)
));
}
Catch (exception ex)
{
Return "";
}
}

}

/// <Summary>
/// Obtain the first letter of a Chinese character based on the location
/// </Summary>
/// <Param name = "gbcode"> Location Code </param>
/// <Returns> </returns>
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 "";
}

}
}

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.