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;
}