Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I truncate a string while converting to bytes in C#?

I would like to put a string into a byte array, but the string may be too big to fit. In the case where it's too large, I would like to put as much of the string as possible into the array. Is there an efficient way to find out how many characters will fit?

like image 559
TimK Avatar asked Aug 29 '08 14:08

TimK


1 Answers

In order to truncate a string to a UTF8 byte array without splitting in the middle of a character I use this:

static string Truncate(string s, int maxLength) {
    if (Encoding.UTF8.GetByteCount(s) <= maxLength)
        return s;
    var cs = s.ToCharArray();
    int length = 0;
    int i = 0;
    while (i < cs.Length){
        int charSize = 1;
        if (i < (cs.Length - 1) && char.IsSurrogate(cs[i]))
            charSize = 2;
        int byteSize = Encoding.UTF8.GetByteCount(cs, i, charSize);
        if ((byteSize + length) <= maxLength){
            i = i + charSize;
            length += byteSize;
        }
        else
            break;
    }
    return s.Substring(0, i);
}

The returned string can then be safely transferred to a byte array of length maxLength.

like image 151
otsdr Avatar answered Sep 17 '22 12:09

otsdr