C # common usage of some string operations

Source: Internet
Author: User

C # common usage of some string operations

// Obtain the location code of Chinese Characters
Byte [] array = newbyte [2];
Array = System. Text. Encoding. Default. GetBytes ("ah ");

Inti1 = (short) (array [0]-''\ 0 '');
Inti2 = (short) (array [1]-''\ 0 '');

// Chinese character codes in unicode decoding mode
Array = System. Text. Encoding. Unicode. GetBytes ("ah ");
I1 = (short) (array [0]-''\ 0 '');
I2 = (short) (array [1]-''\ 0 '');

// Unicode deserialization for Chinese Characters
Stringstr = "4a55 ";
Strings1 = str. Substring (0, 2 );
Strings2 = str. Substring (2, 2 );

Intt1 = Convert. ToInt32 (s1, 16 );
Intt2 = Convert. ToInt32 (s2, 16 );

Array [0] = (byte) t1;
Array [1] = (byte) t2;

Strings = System. Text. Encoding. Unicode. GetString (array );

// Decodes Chinese characters in the default mode
Array [0] = (byte) 196;
Array [1] = (byte) 207;
S = System. Text. Encoding. Default. GetString (array );

// Obtain the string length
S = "iam square gun ";
Intlen = s. Length; // willoutputas6
Byte [] sarr = System. Text. Encoding. Default. GetBytes (s );
Len = sarr. Length; // willoutputas3 + 3*2 = 9

// String Addition
System. Text. StringBuildersb = newSystem. Text. StringBuilder ("");
Sb. Append ("I ");
Sb. Append ("am ");
Sb. Append ("square gun ");

// Obtain the ASC code

Int ASC (String Data)
{
Byte [] B = System. Text. Encoding. Default. GetBytes (Data );
Int p = 0;
If (B. Length = 1) // if it is an English character, return directly
Return (int) B [0];
For (int I = 0; I <B. Length; I ++)
{
P = (int) B [I];
P = p * 256 + B [I + 1]-65536;
}
Return p;
}

/// <Summary> evaluate the string length
/// Evaluate the string length
/// </Summary>
/// <Param name = "inputString"> </param>
/// <Returns> </returns>
Int len (String inputString)
{String s = inputString;
Int len = s. Length; // willoutputa s6
Byte [] sarr = System. Text. Encoding. Default. GetBytes (s );
Len = sarr. Length; // willoutputa s3 + 3*2 = 9
Return len;
}

// Check the actual length of a string containing Chinese Characters
Str is the string to be checked:
ASCIIEncoding n = new ASCIIEncoding ();
Byte [] B = n. GetBytes (str );
Int l = 0; // l is the actual length of the string
For (int I = 0; I <= B. Length-1; I ++)
{
If (B [I] = 63) // determines whether it is a Chinese character or full-legged symbol.
{
L ++;
}
L ++;
}

// Encode the modified string (yaosansi)
/// <Summary>
/// Capture a string
/// </Summary>
/// <Param name = "inputString"> string to be intercepted </param>
/// <Param name = "len"> length of the string to be loaded </param>
/// <Returns> </returns>
Public static string CutString (string inputString, int len)
{

ASCIIEncoding ascii = new ASCIIEncoding ();
Int tempLen = 0;
String tempString = "";
Byte [] s = ascii. GetBytes (inputString );
For (int I = 0; I <s. Length-1; I ++)
{
If (int) s [I] = 63)
{
TempLen + = 2;
}
Else
{
TempLen + = 1;
}

Try
{
TempString + = inputString. Substring (I, 1 );
}
Catch
{
Break;
}

If (tempLen> len)
Break;
}
// If yes, add half ellipsis
Byte [] mybyte = System. Text. Encoding. Default. GetBytes (inputString );
If (mybyte. Length> len)
TempString + = "";

Return tempString;
}

// Extract the string (original)
Public static string CutString (string inputString, int len)
{

ASCIIEncoding ascii = new ASCIIEncoding ();
Int tempLen = 0;
String tempString = "";
Byte [] s = ascii. GetBytes (inputString );
For (int I = 0; I <s. Length; I ++)
{
If (int) s [I] = 63)
{
TempLen + = 2;
}
Else
{
TempLen + = 1;
}

Try
{
TempString + = inputString. Substring (I, 1 );
}
Catch
{
Break;
}

If (tempLen> len)
Break;
}
// If yes, add half ellipsis
Byte [] mybyte = System. Text. Encoding. Default. GetBytes (inputString );
If (mybyte. Length> len)
TempString + = "... ";

Return tempString;
}

// Truncate a Chinese string of the specified length in C #
PrivatestaticstringSubString (stringstringToSub, intlength ){
Regexregex = newRegex ("[\ u4e00-\ u9fa5] +", RegexOptions. Compiled );
Char [] stringChar = stringToSub. ToCharArray ();
StringBuildersb = newStringBuilder ();
IntnLength = 0;

For (inti = 0; I <stringChar. Length; I ++ ){
If (regex. IsMatch (stringChar [I]). ToString ())){
Sb. Append (stringChar [I]);
NLength + = 2;
}
Else {
Sb. Append (stringChar [I]);
NLength = nLength + 1;
}

If (nLength> length)
Break;
}

Returnsb. ToString ();
}
//////////////////////////////////////// /////////////////////////////

String --> bytearray

Byte [] data = Syste. Text. Encoding. ASCII. GetBytes (string );

String --> byte

Bytedata = Convert. ToByte (string );

Byte [] --> string

Stringstring = Encoding. ASCII. GetString (bytes, 0, nBytesSize );

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.