截取帶中文的字符串

          Posted on 2008-02-25 11:38 angel 閱讀(325) 評論(0)  編輯  收藏
          如:abc中國  
          package com;

          class splitstring {
              
          private String str;

              
          private int bytenum;

              
          public splitstring() {
              }


              
          public splitstring(String str, int bytenum) {
                  
          this.str = str;
                  
          this.bytenum = bytenum;

              }


              
          public void splitit() {

                  
          byte bt[] = str.getBytes();
                  
          if (bytenum > 1{
                      
                      
          if(bt[bytenum]<0){
                          String str
          =new String(bt,0,--bytenum);
                          System.out.print(str);
                      }
          else{
                          String str
          =new String(bt,0,bytenum);
                          System.out.print(str);
                      }


                  }
           else {
                      
          if(bytenum==1){
                          
          if(bt[bytenum]<0){
                              String str
          =new String(bt,0,++bytenum);
                              System.out.print(str);
                          }
          else{
                              String str
          =new String(bt,0,bytenum);
                              System.out.print(str);
                          }

                      }
          else{
                          System.out.print(
          "錯誤");    
                      }

                  }

              }

          }


          public class Test {

              
          public static void main(String arg[]) {
                  String str 
          = "我abcd漢def";
                  
          int num = 7;
                  splitstring sptstr 
          = new splitstring(str, num);
                  sptstr.splitit();
              }


          }

          輸出為:我abcd

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          Copyright © angel

          主站蜘蛛池模板: 武胜县| 禄丰县| 浑源县| 伊金霍洛旗| 烟台市| 汉中市| 鸡东县| 马关县| 江城| 买车| 宣威市| 泸州市| 射洪县| 治县。| 四会市| 云南省| 平原县| 舒城县| 邯郸市| 肥乡县| 金阳县| 龙海市| 九台市| 萝北县| 丹东市| 澄迈县| 桐乡市| 阿瓦提县| 鹤山市| 延寿县| 沙坪坝区| 穆棱市| 宜都市| 昆明市| 湟源县| 灵丘县| 巴彦淖尔市| 商丘市| 沈丘县| 化隆| 竹北市|