I am developing it in C#.NET Framework 2.0.
I would like to get the number of bytes in UTF-8 character code for a string and delete it until it falls below the limit when the value exceeds the configured number of bytes limit.
I have considered the following method, but since the number of loops increases in proportion to the size of the string, I am considering a faster and safer way to use the c# function.
Could you please let me know if there is a good way?
Thank you in advance.
string input = "string you want to limit the number of bytes";
UTF8 Encoding utf8 = new UTF8 Encoding (false, true);
int byteSize=utf8.GetByteCount(input);
int maxByteSize=15;
string output = null;
if(byteSize>maxByteSize)
{
inti = 1;
while(true){
output = input.Substring(0, input.Length-i);
if (utf8.GetByteCount(output)<maxByteSize)
{
break;
}
i++;
}
}
else
{
output = input;
}
}
How about encoding byte[]
once, truncating it to the desired number of bytes, and then decoding it ignoring the half-byte string?
string input = "string you want to limit the number of bytes";
int maxByteSize = 14; // I dared to change the number to an incoherent one.
Encoding encoding = Encoding.GetEncoding (65001,
EncoderFallback.ExceptionFallback,//ExceptionFallback.
new DecoderReplacementFallback("")); // Replace errors with empty strings during decoding
bytes[] bytes=encoding.GetBytes(input);
string output = encoding.GetString (bytes, 0, Math.Min (maxByteSize, bytes.Length));
© 2024 OneMinuteCode. All rights reserved.