Rising Sun

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            148 隨筆 :: 0 文章 :: 22 評(píng)論 :: 0 Trackbacks
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(10)

          隨筆分類

          隨筆檔案

          相冊(cè)

          java--->ajax

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          javascript 中文字符長(zhǎng)度判斷

          if(srt.charCodeAt(i) > 255{
          //if(friendnick.charCodeAt(i) < 0x4E00 || friendnick.charCodeAt(i) > 0x9FA5) {
               charlen 
          +=2;
              }
          else{
              
               charlen 
          +=1;
              }

          java 中文字符長(zhǎng)度判斷
           if(tempProperty.getBytes().length > cut*2 ){
                          
          if (tempProperty.length() >= (cut + 1)) {
                              
          byte [] src = tempProperty.getBytes();
                              
          byte [] dest = new byte[cut*2+2];
                              
          int flag = 0;
                              
          for(int j=0;j<src.length;j++){  
                                  
          if(j<= cut*2){
                                     
          // System.out.println(j);
                                      if(src[j]<0)
                                          flag
          ++;
                                      dest[j]
          = src[j];
                                  }

                              }

                            
                              
          if(flag % 2 !=0){
                                  dest[cut
          *2]= 0;
                              }

                              tempProperty 
          =  new String(dest).trim()+ ""
          這個(gè)代碼有問(wèn)題  "sss順磾村厈士大夫"  tempProperty.getBytes() 其中一個(gè)數(shù)字不為負(fù)數(shù)
            修改過(guò)的代碼

           String s = "stss順磾村厈士大夫";
                  String badChar 
          ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                  badChar 
          += "abcdefghijklmnopqrstuvwxyz"
                  badChar 
          += "0123456789"
                  badChar 
          += " "+" ";//半角與全角空格 
                  badChar += ".`~!@#$%^&()-_=+]\\|:;\"\'<,>?/*";
                  String result = "";
                  
          int len = 0;
                      
          for(int i=0;i<s.length();i++)
                          
          char c = s.charAt(i);
                          
          if(badChar.indexOf(c)==-1){//如果是中文
                             result = result+c;
                             len 
          += 2;
                          }
          else{
                             result 
          =  result+c;
                             len 
          += 1;
                          }

                          
          if(len>=15break;
                      }
           
          posted on 2008-10-14 09:22 brock 閱讀(647) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 合阳县| 乌鲁木齐县| 辉县市| 微博| 新营市| 昌乐县| 凉城县| 寿光市| 汶川县| 同仁县| 河间市| 宜州市| 富源县| 杂多县| 昌平区| 永胜县| 富宁县| 万宁市| 谷城县| 九台市| 新河县| 波密县| 阳朔县| 淮北市| 绿春县| 牡丹江市| 冀州市| 洪湖市| 武强县| 龙泉市| 贞丰县| 怀安县| 浦江县| 丹凤县| 麻阳| 集安市| 鸡西市| 福建省| 临颍县| 漯河市| 孙吴县|