ASP如何计算中英文混合字符串长度

阿贝儿儿 2015-01-01 12:20:32
当在搜索输入框中输入中英文混合字符串,点击搜索后 如何能准确用最简单方法捕捉到输入字符串的长度,请教高手指点一下
...全文
259 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_aspnet 2015-01-20
  • 打赏
  • 举报
回复

<script src="JS/DecodeNo.js" type="text/javascript"></script>
    <script type="text/javascript">
        function TransCn() {
            document.getElementById("out").value = ToCn(document.getElementById("Num").value);
        }
        function TransEn() {
            document.getElementById("out").value = ToEn(document.getElementById("Num").value));
        }
    </script>
    <script type="text/javascript">
        function AlertMessage() {
            alert(document.getElementById("Num").value);
        }
    </script>
</head>
<body>
    <form action="" method="get" name="mainForm">
    <input type="text" name="Num" value="" id="Num" />
    <div>
        <input type="button" name="Eng" value="转换成英文 " onclick="TransEn()" /> 
        <input type="button" name="Chinese" value="转换成中文 " onclick="TransCn()" />
    </div>
    <textarea name="out" cols="90" rows="5" id="out"></textarea>
    <input type="button" name="alertMessage" value="Alert Message " onclick="AlertMessage();" />
    </form>
</body>
DecodeNo.js:
function ToCn(a) {
    var b = 9.999999999999E10,
    f = "零",
    h = "壹",
    g = "贰",
    e = "叁",
    k = "肆",
    p = "伍",
    q = "陆",
    r = "柒",
    s = "捌",
    t = "玖",
    l = "拾",
    d = "佰",
    i = "仟",
    m = "万",
    j = "亿",
    u = "",
    o = "元",
    c = "角",
    n = "分",
    v = "整";
    
    if (a == "") {
        alert("请输入数字!");
        return ""
    }
    if (a.match(/[^,.\d]/) != null) {
        alert("请不要输入其他字符!");
        return ""
    }
    if (a.match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) ==
    null) {
        alert("非法格式!");
        return ""
    }
    a = a.replace(/,/g, "");
    a = a.replace(/^0+/, "");
    if (Number(a) > b) {
        alert("对不起,你输入的数字太大了!最大数字为99999999999.99!");
        return ""
    }
    b = a.split(".");
    if (b.length > 1) {
        a = b[0];
        b = b[1];
        b = b.substr(0, 2)
    } else {
        a = b[0];
        b = ""
    }
    h = new Array(f, h, g, e, k, p, q, r, s, t);
    l = new Array("", l, d, i);
    m = new Array("", m, j);
    n = new Array(c, n);
    c = "";
    if (Number(a) > 0) {
        for (d = j = 0; d < a.length; d++) {
            e = a.length - d - 1;
            i = a.substr(d,
            1);
            g = e / 4;
            e = e % 4;
            if (i == "0") j++;
            else {
                if (j > 0) c += h[0];
                j = 0;
                c += h[Number(i)] + l[e]
            }
            if (e == 0 && j < 4) c += m[g]
        }
        c += o
    }
    if (b != "") for (d = 0; d < b.length; d++) {
        i = b.substr(d, 1);
        if (i != "0") c += h[Number(i)] + n[d]
    }
    if (c == "") c = f + o;
    if (b.length < 2) c += v;
    return c = u + c
}
var arr1 = new Array("", " thousand", " million", " billion"),
arr2 = new Array("zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"),
arr3 = new Array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"),
arr4 = new Array("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen");
function ToEn(a) {
    var b = a.length,
    f,
    h = 0,
    g = "",
    e = Math.ceil(b / 3),
    k = b - e * 3;
    g = "";
    for (f = k; f < b; f += 3) {++h;
        num3 = f >= 0 ? a.substring(f, f + 3) : a.substring(0, k + 3);
        strEng = English(num3);
        if (strEng != "") {
            if (g != "") g += ",";
            g += English(num3) + arr1[e - h]
        }
    }
    return g
}
function English(a) {
    strRet = "";
    if (a.length == 3 && a.substr(0, 3) != "000") {
        if (a.substr(0, 1) != "0") {
            strRet += arr3[a.substr(0, 1)] + " hundred";
            if (a.substr(1, 2) != "00") strRet += " and "
        }
        a = a.substring(1)
    }
    if (a.length == 2) if (a.substr(0, 1) == "0") a = a.substring(1);
    else if (a.substr(0, 1) == "1") strRet += arr4[a.substr(1, 2)];
    else {
        strRet += arr2[a.substr(0, 1)];
        if (a.substr(1, 1) != "0") strRet += "-";
        a = a.substring(1)
    }
    if (a.length == 1 && a.substr(0, 1) != "0") strRet += arr3[a.substr(0, 1)];
    return strRet
};
function copy(a) {
    if (a = findObj(a)) {
        a.select();
        js = a.createTextRange();
        js.execCommand("Copy")
    }
}
function findObj(a, b) {
    var c,
    d;
    b || (b = document);
    if ((c = a.indexOf("?")) > 0 && parent.frames.length) {
        b = parent.frames[a.substring(c + 1)].document;
        a = a.substring(0, c)
    }
    if (! (d = b[a]) && b.all) d = b.all[a];
    for (c = 0; ! d && c < b.forms.length; c++) d = b.forms[c][a];
    for (c = 0; ! d && b.layers && c < b.layers.length; c++) d = findObj(a, b.layers[c].document);
    if (!d && document.getElementById) d = document.getElementById(a);
    return d
}
  • 打赏
  • 举报
回复
引用 4 楼 abeier009 的回复:
弄不明白楼上写的是怎么弄的 比如我搜索框代码如下 <form name="form" method="post" action="123.asp" > <input name="a" id="a" type="text" > <input type="submit"> </form> 点击搜索进入123.asp后 a = Request.form("a") 然后我怎么写代码获得a长度
Public function StrLength(str)
   Dim Rep,lens,i
   Set rep=new regexp
   rep.Global=true
   rep.IgnoreCase=true
   rep.Pattern="[\u4E00-\u9FA5\uF900-\uFA2D]"
   For each i in rep.Execute(str)
    lens=lens+1
   Next
   Set Rep=Nothing
   lens=lens + len(str)
   strLength=lens
End Function
ASP函数名:StrLength 作用:获取指定字符串的长度(汉字为2个字节) 函数参数:str ——需要统计长度的字符串内容 返回值:字符串长度,一般是数字值 这样: a = Request.form("a") <%=StrLength(a)%>
阿贝儿儿 2015-01-20
  • 打赏
  • 举报
回复
弄不明白楼上写的是怎么弄的 比如我搜索框代码如下 <form name="form" method="post" action="123.asp" > <input name="a" id="a" type="text" > <input type="submit"> </form> 点击搜索进入123.asp后 a = Request.form("a") 然后我怎么写代码获得a长度
阿贝儿儿 2015-01-20
  • 打赏
  • 举报
回复
Encoding.Default.GetBytes(str).Length / 2 + 1 是什么意思啊
HerringYang 2015-01-20
  • 打赏
  • 举报
回复
Encoding.Default.GetBytes(str).Length / 2 + 1
阿贝儿儿 2015-01-20
  • 打赏
  • 举报
回复
代码按下边写出来 <% function StrLength(str) Dim Rep,lens,i Set rep=new regexp rep.Global=true rep.IgnoreCase=true rep.Pattern="[\u4E00-\u9FA5\uF900-\uFA2D]" For each i in rep.Execute(str) lens=lens+1 Next Set Rep=Nothing lens=lens + len(str) strLength=lens End Function %> <% a = "中" %> <%=StrLength(a)%> 当 a = "中" StrLength(a)值显示为 Microsoft VBScript ���������� ���� '800a0409' 当 a = "中国" StrLength(a)值显示为 5 当 a = "中国人" StrLength(a)值显示为 Microsoft VBScript ���������� ���� '800a0409' 当 a = "中国人民" StrLength(a)值显示为 11 当 a = "中国人的民" StrLength(a)值显示为 Microsoft VBScript ���������� ���� '800a0409' 当 a = "中国adfg123的民" StrLength(a)值显示为 18 当 a = "adfg123" StrLength(a)值显示为 7 从这里可以看出当汉字是奇数时 显示出错误代码 当汉字是偶数时 显示字符串长度不准确 当a为英文和数字是准确的
csdn_aspnet 2015-01-04
  • 打赏
  • 举报
回复
function strLength(str)
       ON ERROR RESUME NEXT
       dim WINNT_CHINESE
       WINNT_CHINESE    = (len("论坛")=2)
       if WINNT_CHINESE then
          dim l,t,c
          dim i
          l=len(str)
          t=l
          for i=1 to l
             c=asc(mid(str,i,1))
             if c<0 then c=c+65536
             if c>255 then
                t=t+1
             End if
          next
          strLength=t
       else 
          strLength=len(str)
       End if
       if err.number<>0 then err.clear
End function
xcgh 2015-01-01
  • 打赏
  • 举报
回复
http://blog.csdn.net/citybird/article/details/4529658
三楼の郎 2015-01-01
  • 打赏
  • 举报
回复
引用 1 楼 xcgh 的回复:
http://blog.csdn.net/citybird/article/details/4529658
头一回碰到有人引用我的文章,深感荣幸

28,394

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧