Best way to shorten UTF8 string based on byte length

前端 未结 9 1420
感情败类
感情败类 2020-12-10 12:14

A recent project called for importing data into an Oracle database. The program that will do this is a C# .Net 3.5 app and I\'m using the Oracle.DataAccess connection libra

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 12:27

    This is another solution based on binary search:

    public string LimitToUTF8ByteLength(string text, int size)
    {
        if (size <= 0)
        {
            return string.Empty;
        }
    
        int maxLength = text.Length;
        int minLength = 0;
        int length = maxLength;
    
        while (maxLength >= minLength)
        {
            length = (maxLength + minLength) / 2;
            int byteLength = Encoding.UTF8.GetByteCount(text.Substring(0, length));
    
            if (byteLength > size)
            {
                maxLength = length - 1;
            }
            else if (byteLength < size)
            {
                minLength = length + 1;
            }
            else
            {
                return text.Substring(0, length); 
            }
        }
    
        // Round down the result
        string result = text.Substring(0, length);
        if (size >= Encoding.UTF8.GetByteCount(result))
        {
            return result;
        }
        else
        {
            return text.Substring(0, length - 1);
        }
    }
    

提交回复
热议问题