姿姿霸霸~~!
          貴在堅持!
          posts - 106,  comments - 50,  trackbacks - 0
          /**
               * 按照指定長度將字符串進行分割,中文字符算2個長度
               * 
          @param str 字符串
               * 
          @param length 指定長度
               * 
          @return 如果字符串長度超出指定長度
               * ,則將字符串分成2個部分,分別裝在map中
               
          */

              
          public static Map getStr(String str, int length) {
                  HashMap hashMap 
          = new HashMap();
                  String addr1 
          = "";
                  String addr2 
          = "";

                  
          byte tmpBytes[] = str.getBytes();
                  
          int iByteLen = tmpBytes.length;
                  
          if (iByteLen > length) {
                      
          int iLen = 0;
                      
          for (int i = 0; i < length; i++{
                          
          if ((tmpBytes[i] & 0xFF> 0x80{
                              iLen 
          += 2;
                              i
          ++;
                              
          continue;
                          }
           else {
                              iLen 
          += 1;
                              
          continue;
                          }

                      }


                      addr1 
          = new String(tmpBytes, 0, iLen);
                      addr2 
          = new String(tmpBytes, iLen, iByteLen - iLen);
                  }
           else {
                      addr1 
          = str;
                  }

                  hashMap.put(
          new Integer(1), addr1);
                  hashMap.put(
          new Integer(2), addr2);
                  
          return hashMap;
              }


          0x80等于十進制的128,Turbo C中規定對ASCII碼值大于0x80的字符將被認為是負數。
          posted on 2009-09-22 22:32 xrzp 閱讀(2152) 評論(1)  編輯  收藏 所屬分類: JAVA

          FeedBack:
          # re: 按長度分割字符串,遇到中文的處理
          2009-09-22 23:01 | Jcat
          好久沒更新咯  回復  更多評論
            

          <2009年9月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          好友的blog

          搜索

          •  

          積分與排名

          • 積分 - 118088
          • 排名 - 499

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高淳县| 兰西县| 恩平市| 盘锦市| 兖州市| 甘德县| 三穗县| 太原市| 巴南区| 玉树县| 大同县| 惠安县| 富民县| 西充县| 嘉黎县| 新河县| 平阳县| 海门市| 九江县| 岳西县| 都昌县| 灵丘县| 德令哈市| 晋宁县| 扶沟县| 定南县| 额济纳旗| 库尔勒市| 城口县| 新巴尔虎左旗| 杨浦区| 海原县| 白玉县| 察哈| 全南县| 屯昌县| 循化| 竹山县| 两当县| 牙克石市| 襄城县|