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; 
        }
  • 相关阅读:
    Bluedroid介绍
    Android蓝牙介绍
    Android Bluetooth抓包
    Bluetooth LMP介绍
    Bluetooth Baseband介绍
    Bluetooth SDP介绍
    Bluetooth HFP介绍
    Bluetooth RFCOMM介绍
    Bluetooth L2CAP介绍
    Windows开发
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/3045076.html
Copyright © 2011-2022 走看看