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

Y2J
Release: 2017-04-27 09:24:04
Original
2114 people have browsed it

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++; 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template