开发语言:JavaScript
思想:先用正则表达式判断是否汉字,如是的,则用空字符取代,再用取代前字符
长度减去取代后长度,就是汉字的字数
代码:
1
<script language="javascript">
2
function ShowLen()
3
>
统计字符串中的汉字的字数,也可以是其它字符之类2
3
c#:
1
string sInput, sRegex;
2
// The string to search.
3
sInput = "1.4 整数2321不能大于iMax";
4
// A very simple regular expression.
5
sRegex = "[\u4e00-\u9fa5]";
6
Regex r = new Regex(sRegex);
7
Match m=r.Match (sInput);
8
int i=0;
9
while (m.Success )
10
.Response .Write (i.ToString ());
2
3
4
5
6
7
8
9
10