Home  >  Article  >  Backend Development  >  Teach you how to use C# to detect the actual length of a string containing Chinese characters

Teach you how to use C# to detect the actual length of a string containing Chinese characters

Y2J
Y2JOriginal
2017-04-27 09:24:041989browse

In the actual process, we often need to detect the actual length of the Chinese string,
Because Chinese characters and English characters are together, it will be more troublesome for you to judge.
The principle is the same as in asp .Judge one by one. It means Chinese plus 2 English plus 1.

The code is as follows:

ASCIIEncoding n = new ASCIIEncoding(); 
byte[] b = n.GetBytes(str); 
int l = 0; // l 为字符串之实际长度 
for (int i=0;i <= b.Length-1;i++) 
{ 
if (b[i] ==63) //判断是否为汉字或全脚符号 
{ 
l++; 
} 
l++; 
}

The above is the detailed content of Teach you how to use C# to detect the actual length of a string containing Chinese characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn