有色人生 发表于 2009-1-22 16:49:32

编写一个截取字符串的函数的截取

编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC 汉的半个".
class SplitString
{
    String SplitStr;
    int SplitByte;
    public SplitString(String str,int bytes)
    {
      SplitStr=str;
      SplitByte=bytes;
      System.out.println("The String is:′" SplitStr "′;SplitBytes=" SplitByte);
    }
    public void SplitIt()
    {
      if(SplitByte>=2){
            byte [] splbyte=SplitStr.getBytes();
            int i=0;
            while(i<splbyte.length){
                String str=null;
                byte []tempbyte=new byte;
                for(int j=0;j<tempbyte.length;){
                  if(i<splbyte.length){
                        if(splbyte>0){
                            tempbyte=splbyte;
                            i;
                            j;
                        }else{
                            if(tempbyte.length-j==1){
                              j;
                            }else{
                              tempbyte=splbyte;
                              i;
                              j;
                              tempbyte=splbyte;
                              i;
                              j;
                            }
                        }
                  }else{
                        break;
                  }
                }
                str=new String(tempbyte);
                System.out.println(str);
            }
      }else{
            System.out.println("输入参数不能小于2");
      }
    }
    public static void main(String[] args)
    {
      SplitString ss = new SplitString("文dsf中kdjaa地0方0地0方l",6);
      ss.SplitIt();
    }
}
页: [1]
查看完整版本: 编写一个截取字符串的函数的截取