C # string truncation by byte

Source: Internet
Author: User

C # string truncation by byte

/// <Summary>
/// Intercept a string of the specified byte length
/// </Summary>
/// <Param name = "str"> original string </param>
/// <Param name = "startindex"> Start position </param>
/// <Param name = "len"> truncated bytes </param>
/// <Returns> </returns>
Public static string cutbytestring (string STR, int startindex, int Len)
{
String result = string. Empty; // The final returned result.
If (string. isnullorempty (STR) {return result ;}


Int bytelen = system. Text. encoding. Default. getbytecount (STR); // length of a single byte
Int charlen = Str. length; // String Length when the characters are treated equally


If (startindex = 0)
{
Return cutbytestring (STR, Len );
}
Else if (startindex> = bytelen)
{Return result ;}
Else // startindex <bytelen
{
Int alllen = startindex + Len;
Int bytecountstart = 0; // record the read progress
Int bytecountend = 0; // record the read progress
Int startpos = 0; // record position
Int endpos = 0; // record position
For (INT I = 0; I <charlen; I ++)
{
If (convert. toint32 (Str. tochararray () [I])> 255) // calculate by Chinese Character and add 2
{Bytecountstart + = 2 ;}
Else // Add 1 to the English character
{Bytecountstart + = 1 ;}
If (bytecountstart> startindex) // when the value exceeds the upper limit, only the previous valid position is recorded.
{
Startpos = I;
Alllen = startindex + len-1;
Break;
}
Else if (bytecountstart = startindex) // write down the current location
{
Startpos = I + 1;
Break;
}
}


If (startindex + Len <= bytelen) // The length of the intercepted characters is less than the total length.
{
For (INT I = 0; I <charlen; I ++)
{
If (convert. toint32 (Str. tochararray () [I])> 255) // calculate by Chinese Character and add 2
{Bytecountend + = 2 ;}
Else // Add 1 to the English character
{Bytecountend + = 1 ;}
If (bytecountend> alllen) // when the value exceeds the upper limit, only the previous valid position is recorded.
{
Endpos = I;
Break;
}
Else if (bytecountend = alllen) // write down the current location
{
Endpos = I + 1;
Break;
}
}
Endpos = endpos-startpos;
}
Else if (startindex + Len> bytelen) // The length of the truncated characters exceeds the length.
{
Endpos = charlen-startpos;
}
If (endpos> = 0)
{Result = Str. substring (startpos, endpos );}
}
Return result;
}
/// <Summary>
/// Intercept a string of the specified byte length
/// </Summary>
/// <Param name = "str"> original string </param>
/// <Param name = "len"> truncated bytes </param>
/// <Returns> </returns>
Public static string cutbytestring (string STR, int Len)
{
String result = string. Empty; // The final returned result.
If (string. isnullorempty (STR) {return result ;}
Int bytelen = system. Text. encoding. Default. getbytecount (STR); // length of a single byte
Int charlen = Str. length; // String Length when the characters are treated equally
Int bytecount = 0; // record the read progress
Int Pos = 0; // record position
If (bytelen> Len)
{
For (INT I = 0; I <charlen; I ++)
{
If (convert. toint32 (Str. tochararray () [I])> 255) // calculate by Chinese Character and add 2
{Bytecount + = 2 ;}
Else // Add 1 to the English character
{Bytecount + = 1 ;}
If (bytecount> Len) // when the value exceeds the upper limit, only the previous valid position is recorded.
{
Pos = I;
Break;
}
Else if (bytecount = Len) // write down the current location
{
Pos = I + 1;
Break;
}
}
If (Pos> = 0)
{Result = Str. substring (0, POS );}
}
Else
{Result = STR ;}
Return result;
}

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.