zoukankan      html  css  js  c++  java
  • JS/Java中,判断字符串是否包含中文

    网络上类似的代码一般都是JS判断字符串是否全为中文,但判断是否包含中文的代码很少,这里提供三种方法:

    <script language="javascript">  
    function funcChina(){ 
    var obj = document.form1.txtName.value; 
    if(/.*[\u4e00-\u9fa5]+.*$/.test(obj)) 
    { 
    alert("不能含有汉字!"); 
    return false; 
    } 
    return true; 
    } 
    </script> 
    <form name="form1">
    <input type="text" name="txtName"><input type="button" name="butTxt" value="判断是否是汉字" onclick="funcChina()">
    </form>
    <script language="javascript"> 
    function isChina(s){ 
    var patrn=/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi; 
    if(!patrn.exec(s)){ 
    return false; 
    }
    else{ 
    return true; 
    } 
    }
    alert(isChina("中国站长天空www.zzsky.cn"));
    </script>

     

    <script language="javascript">
    var str='中国站长天空www.zzsky.cn';
    if(escape(str).indexOf("%u")<0){ 
    alert("没有包含中文");
    }
    else{
    alert("包含中文");
    }
    </script>

    Java中:

        /**
         * 判断str是否为中文字符.
         * 
         * @param str
         * @return
         */
        private static boolean chineseEncode(String str){
            
            char[] chars=str.toCharArray(); 
            boolean isGB2312=false; 
            for(int i=0;i<chars.length;i++){
                        byte[] bytes=(""+chars[i]).getBytes(); 
                        if(bytes.length==2){ 
                                    int[] ints=new int[2]; 
                                    ints[0]=bytes[0]& 0xff; 
                                    ints[1]=bytes[1]& 0xff; 
                                    if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){ 
                                                isGB2312=true; 
                                                break; 
                                    } 
                        } 
            } 
            return isGB2312; 
        }
  • 相关阅读:
    LeetCode:387字符串中唯一出现一一次的字符
    LeetCode-79. 单词搜索
    LeetCode-75. 颜色分类
    LeetCode-121. 买卖股票的最佳时机
    LeetCode-58. 最后一个单词的长度
    LeetCode-1103. 分糖果 II
    LeetCode:283. 移动零
    LeetCode:38. 外观数列
    LeetCode:70. 爬楼梯
    获取美拍视频的链接--JS分析
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/3045076.html
Copyright © 2011-2022 走看看