截取帶中文的字符串

          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

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


          網(wǎng)站導(dǎo)航:
           

          Copyright © angel

          主站蜘蛛池模板: 腾冲县| 裕民县| 和政县| 安多县| 桑植县| 阿拉善右旗| 阿克苏市| 泸水县| 高清| 富川| 石台县| 睢宁县| 莱阳市| 越西县| 六盘水市| 京山县| 沾益县| 泗水县| 广丰县| 台东市| 迁安市| 天全县| 太仆寺旗| 高青县| 谢通门县| 称多县| 三亚市| 家居| 新兴县| 元江| 九江市| 钟山县| 南投县| 如东县| 定安县| 新源县| 铜川市| 永登县| 凤庆县| 营山县| 青神县|