Detect the actual length of a string containing Chinese characters

巴扎黑
Release: 2017-05-21 13:55:01
Original
1274 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 Detect the actual length of a string containing Chinese characters. For more information, please follow other related articles on the PHP Chinese website!

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