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)航:
           
          主站蜘蛛池模板: 绍兴县| 株洲市| 北票市| 定陶县| 屏山县| 且末县| 公安县| 江口县| 尼木县| 丹巴县| 自贡市| 札达县| 乌什县| 弥渡县| 望城县| 曲阜市| 天峨县| 福建省| 称多县| 通海县| 玉山县| 马边| 德保县| 大石桥市| 沙湾县| 陆河县| 连江县| 九龙县| 昔阳县| 乐陵市| 舟曲县| 揭东县| 内江市| 尼木县| 房山区| 抚顺市| 无为县| 邳州市| 柳林县| 客服| 萨迦县|