日期:2014-05-20 浏览次数:21013 次
  public static void split(String source,int num) throws Exception
  {
    int k=0;
    String temp="";
    for (int i = 0; i <source.length(); i++)
    {
      byte[] b=(source.charAt(i)+"").getBytes();
      k=k+b.length;
      if(k>num)
      {
        break;
      }
      temp=temp+source.charAt(i);
    }
    System.out.println(temp);
  }
------解决方案--------------------
    public static void main(String[] args) {
        String str = "我ABC汉DEF";
        System.out.println(subString(str, 6));
    }
    public static String subString(String str, int length){
        char[] chars = str.toCharArray();
        int temp = 0;
        int index = 0;
        while(temp <= length){
            if(index >= chars.length)
                return str;
            if(chars[index++] > 255)
                temp += 2;
            else
                temp += 1;
        }
        return str.substring(0, index - 1);
    }