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!