Detects the actual length of a string containing Chinese characters

  • 2020-05-05 11:05:49
  • OfStack

In the actual process, we often need to check the actual length of the Chinese string,
Because of the Chinese characters, and English characters together, you are a bit more trouble to judge.
The principle is the same as in asp

ASCIIEncoding n = new ASCIIEncoding(); 
byte[] b = n.GetBytes(str); 
int l = 0; // l  Is the actual length of the string  
for (int i=0;i <= b.Length-1;i++) 
{ 
if (b[i] ==63) // Determine whether it is a Chinese character or a full-foot symbol  
{ 
l++; 
} 
l++; 
}

Related articles: