编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
这题以前在论坛里看到过,,记得当时有一种很短的算法,好像只是用了一个while循环。找了半天没找着,希望大家有知道帮帮忙,谢谢大家了

解决方案 »

  1.   


    public class string {
    public static void main(String[] args){
    byte[] b="我ABC汉DEF".getBytes();//输入一个字符串,将它转换成字节数组
    int n=4;//设置你要截取的字节数
    String str=new String(b,0,n);//重新创建一个字符串,b是字符创内容,0是截取前指针指向的位置,n是截取字节个数
    System.out.println(str);//将截取的字符串打印出来

    }
    }
    这个比你用while的还简单
      

  2.   

    Ls的,要是你的n=6的时候看下输出的结果。
      

  3.   

    看下这个:http://www.cnblogs.com/zhonghan/archive/2009/04/23/1442241.html
    这个是在你的题目基础上的另外一个类似的题目:http://topic.csdn.net/u/20091210/23/58a37291-fd1a-4fd0-85b7-791f1d75cb7f.html
      

  4.   


    public String getString(String s,int n) throws UnsupportedEncodingException{
       int index = 0;    //定义游标位置
       StringBuffer ss = new StringBuffer();
       for(int i = 0 ; i < n; i++){
        if(s.charAt(index)<255 && s.charAt(index)>0 || Character.isDigit(s.charAt(index))){      ss.append(s.charAt(index));     //如果当前字符不输于字母或者是数字,则添加到结果。
         index++;
        }else{
         ss.append(s.charAt(index));    //如果当前字符是汉字,则添加到结果中,游标向前移动一位。
         index++;
         i++;
        }
       }
       return ss.toString();
    }
      

  5.   

    这里发过贴
    http://topic.csdn.net/u/20090307/17/564fe3b5-158d-4141-803f-67cd420fb3be.html
      

  6.   

    public class test{      public void splitIt(String splitStr, int bytes) {  
        int cutLength = 0;  
        int byteNum = bytes;  
        byte bt[] = splitStr.getBytes();  
        System.out.println("Length of this String ===>" + bt.length);  
        if (bytes > 1) {  
        for (int i = 0; i < byteNum; i++) {  
        if (bt[i] < 0) {  
        cutLength++;      }  
        }      if (cutLength % 2 == 0) {  
        cutLength /= 2;  
        }else  
        {  
        cutLength=0;  
        }  
        }  
        int result=cutLength+--byteNum;  
        if(result>bytes)  
        {  
        result=bytes;  
        }  
        if (bytes == 1) {  
        if (bt[0] < 0) {  
        result+=2;      }else  
        {  
        result+=1;  
        }  
        }  
        String substrx = new String(bt, 0, result);  
        System.out.println(substrx);      }      public static void main(String args[]) {  
        String str = "我abc的DEFe呀fgsdfg大撒旦";  
        int num =3;  
        System.out.println("num:" + num);  
        test sptstr = new test();  
        sptstr.splitIt(str, num);  
        }      } 
    运行情况:
    num:3 
    Length of this String ===>25 
    我a num:2 
    Length of this String ===>25 
    我 num:1 
    Length of this String ===>25 
    我 num:4 
    Length of this String ===>25 
    我ab
      

  7.   

    编码确定是UTF-8的话,0x0000~0x00ff都是常用的窄字符,一个循环确实可以做到。
      

  8.   

    package test; 
    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() 

    int loopCount; 
    loopCount=(SplitStr.length()%SplitByte==0)?(SplitStr.length()/SplitByte):(SplitStr.length()/Split Byte+1); 
    System.out.println("Will Split into "+loopCount); 
    for (int i=1;i<=loopCount ;i++ ) 

    if (i==loopCount){ 
    System.out.println(SplitStr.substring((i-1)*SplitByte,SplitStr.length())); 
    } else { 
    System.out.println(SplitStr.substring((i-1)*SplitByte,(i*SplitByte))); 



    public static void main(String[] args) 

    SplitString ss = new SplitString("test中dd文dsaf中男大3443n中国43中国人 0ewldfls=103",4); 
    ss.SplitIt(); 

    }
    好不容易找到了呵
      

  9.   

    今天下午抽点时间做了做。想了一个方法。public static String split(String str,int n) {   int count = 0 ;
       String _str = "" ;
       for (int i=0;i<str.length();i++) {
            
          //先判断每一个字符是不是汉字
           if (str.charAt(i).getBytes().length == 2) {
              count += 2 ;
          } else {
              count ++ ;
          }  
          if (count > n) {
               break ;
          }
          _str += str.subString(i,i+1) ;
       } 
       return _str ;
    }
      

  10.   

    如果是汉字的话。str.charAt(i).getBytes().length == 2  这就是解题的关键!汉字得到的 getBytes()的长度是2.
      

  11.   

    这是一道经典的面试题,做过,今天不是在自己的电脑上上网,所以源码没带,但是抓住汉字是两个字节这个关键点,以要求字节数为结束条件用while循环就可以做出......
      

  12.   

    我不为楼主这样的标题所吸引,也不是被帖子的内容所迷惑。我不是来抢沙发的,也不是来打酱油的。我不是为楼主呐喊加油的,也不是对楼主进行围堵攻击的。我只是为了每天30帖默默奋斗。你是个美女,我毫不关心,你是个怪兽,我决不在意;你是个帅哥,我不会妒忌,你是个畜男,我也不会PS。你的情操再怎么高尚,我也不会赞美,你的道德如何沦丧,我也不为所动。在这个处处都要银币的时代,不得不弄个牛B的数字来显眼,于是我抄下了这段话,专门用来回帖,好让我每天有固定的积分收入
      

  13.   


                   String str = "我ABC汉DEF";
    byte leng = 6;
    char[] len = str.toCharArray();
    byte[] by = str.getBytes();
    StringReader sr = new StringReader(str);
    int lon = sr.read(len, 0, leng-1);
    System.out.println(new String(by,0,lon));
      

  14.   

    我发现汉字的byte都是负数 
    public static String getSubStr(String s,int n){
    byte[] bb = s.getBytes();
    byte[] result=new byte[n];

    if(bb[n-1]<0){
    bb[n-1]='\0';
    }
    for(int i=0;i<n;i++){
    result[i]=bb[i];
    }

    return new String(result);
    }
      

  15.   


    package com.yhw.test;import java.util.ArrayList;
    import java.util.List;class SplitString {
    /*
     * 首先按字节数bytes截取字符串splitStr
     * 然后判断截取的字符串temp的总字节数是否符合条件,若是则直接加到list中,
     * 否则循环地去掉temp最后一个字符,直到符合条件为止
     */
    public List<String> SplitStr(String splitStr, int bytes) {
    List<String> list = new ArrayList<String>();
    int beginIndex = 0;
    int endIndex = 0;
    while (true) {
    String temp;
    endIndex = beginIndex + bytes;
    if (endIndex >= splitStr.length()) {
    endIndex = splitStr.length();
    temp = splitStr.substring(beginIndex, splitStr.length());
    while (true) {
    if (temp.getBytes().length > bytes) {
    temp = temp.substring(0, temp.length() - 1);
    } else {
    beginIndex += temp.length();
    list.add(temp);
    break;
    }
    }
    temp = splitStr.substring(beginIndex, splitStr.length());
    list.add(temp);
    break;
    }
    temp = splitStr.substring(beginIndex, endIndex);
    while (true) {
    if (temp.getBytes().length > bytes) {
    temp = temp.substring(0, temp.length() - 1);
    } else {
    beginIndex += temp.length();
    list.add(temp);
    break;
    }
    }
    }
    return list;
    } public static void main(String[] args) {
    SplitString ss = new SplitString();
    String splitStr = "YouAre中国China人民哈哈哈";
    int bytes = 4;
    List<String> list = new ArrayList<String>();
    list = ss.SplitStr(splitStr, bytes);
    for (String s : list) {
    System.out.println(s);
    }

    }
    }