SoFunction
Updated on 2025-03-07

Detailed explanation of how to judge the display width of a string in C#

cause

The company has a project that uses HTML to convert it into PDF. There is a table. The cell in the last column of the table will be discarded after the string is too long, instead of wrapping the line to the next line to display (there is no way to change it due to the HtmlToPdf rendering engine)

Solution

Add manually according to the length of the string<br/>Line break

var source = "ABCD";
 
if (GetLength(source)&gt;2)
{
    source = (2,"&lt;br/&gt;");
}
 
(source);
// Output AB<br/>CD 
int GetLength(string src)
{
    return ;
}

A bug appeared

When Chinese exists in the original content, the display width of Chinese characters is greater than the width of English characters, and it is processed according to the number of characters in the string, which is not accurate

/*
 * Chinese: Chinese<br/>CD
 * English: AB<br/>CD
 */

Cause analysis

In the display of text, there is a difference between full and half-width

Full-width: refers to the state in which one character occupies two standard character positions.

Half-width: refers to the state in which a character occupies a standard character position.

Try to solve it

Character encoding: The earliest character encoding was Ascii code, which only considered users of English language. Later, with the popularity of computers, other encodings were found, such as GB2312, UTF8, etc., which not only contained English character encodings, but these encodings were compatible with Ascii codes.

The conclusion without careful verification: Ascii code corresponds to half-width, the Chinese extended part is displayed in full-width, and the half-width display width is half of the full-width (I guess this conclusion, there is no verification. If it is not correct, please criticize and correct it)

Through the above guessing conclusion, you can first determine whether it is an Ascii character for each character to determine whether it is currently a full or a half angle.

var source = "ABCD";
var index =InserAtDisplayWidth(2,source);
if (index!=-1)
{
    source = (index,"&lt;br/&gt;");
}
// AB&lt;br/&gt;CD
(source);
 
var source2 = "Chinese CD"; 
var index2 =InserAtDisplayWidth(2,source2);
if (index2!=-1)
{
    source2 = (index2,"&lt;br/&gt;");
}
// Chinese<br/>CD(source2);
 
 
 
int InserAtDisplayWidth(int inserAtDisplayWidth, string source){
    int now =0;
    for (int i = 0; i &lt; ; i++)
    {
        if( (source[i])){
            // Half of the corner occupies a display width            now +=1;
        }else{
            // The full width of the display            now +=2;
        };
 
        if (now&gt;inserAtDisplayWidth)
        {
            return i;
        }
    }
    return -1;
}

Through the judgment of characters, the separated positions are more accurately matched, which not only avoids invalid blank areas, but also avoids overflowing of excessive strings, resulting in inability to see content

/*
 * AB<br/>CD
 * Chinese<br/> CD
 */

This is the end of this article about how to judge the display width of a string in detail. For more related contents of C# to determine the display width of a string, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!