A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马王晓明 中级黑马   /  2013-8-10 20:35  /  1470 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 黑马王晓明 于 2013-8-11 13:34 编辑

输入一个字符串和字节数,输出为按字节截取的字符串。
但是要保证汉字不被截半个,如“我ABC”,4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”

4 个回复

倒序浏览
mport java.io.UnsupportedEncodingException;
public class TestC {

public String subStrByBytes(String input, int subLength)
   throws UnsupportedEncodingException {
  // 总的字符数,A是一个字符,汉 也只是一个字符
  int totalCharacter = input.length();
  // 总的字节数
  int totalBytes = input.getBytes("GBK").length;
  System.out.println("total bytes:" + input.getBytes("GBK").length
    + ",total character: " + totalCharacter);
  if (subLength > totalBytes) {
   System.err.println("input length error!please check.");
   return "";
  }
  int current = 0;
  String result = "";
  // 遍历字符串,直到截取目标长度
  for (int i = 0; i < totalCharacter; i++) {
   String tmp = input.substring(i, i + 1);
   int c_len = tmp.getBytes("GBK").length;
   if (current < subLength) {
    current += c_len;
    result += tmp;
   }
  }
  System.out.println("result==" + result);
  return result;
}
public static void main(String[] args) throws UnsupportedEncodingException {
  String str = "我ABC,中<汉DEF";
  int len = 7;
  TestC demo = new TestC();
  String out = demo.subStrByBytes(str, len);
  System.out.println(str + "   >>get " + len + " bytes is :     " + out);
}
}
楼主看下这个答案满意吗

评分

参与人数 1技术分 +1 收起 理由
薛鹏鹏 + 1

查看全部评分

回复 使用道具 举报
yan 发表于 2013-8-10 22:01
mport java.io.UnsupportedEncodingException;
public class TestC {

没学IO呢,这个题还有别的方法吗
回复 使用道具 举报
黑马王晓明 发表于 2013-8-11 00:19
没学IO呢,这个题还有别的方法吗

有点看不懂你写的这些 目前水平只能到达集合这里 有没有能用集合之前的内容实现这个功能呢?
回复 使用道具 举报
哈哈我把我的发给你看看吧,我是这么做的。
package com.itheima;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Text_10 {
/*
  * 思路:
  * 1,建立一个接受键盘输入的字符串方法
  * 2,建立接受键盘输入的需要截取位数的方法,方法中要判断是否是数字,通过判断字符码(48-57)的方式盘判断是否是数字,
  *   错了要重新输入。
  * 3,建立方法来截取,输出,返回
  *   (1)建立接受进来的字符串的字节数组
  *     (2)由于汉字的编码是负数,这就提供了截取的最后以为要不要舍去的依据。
  *   (3)通过接受进来的需要截取的位数来判断在字节数组中初步的位置(index),然后从index位置开始向前面数,看看有多少位
  *    负值(num)
  *   (4)判断num的奇偶性,如果是奇数那么就舍去最后一个字节建立新的需要返回的子串,反之则不用舍去最后一位。
  */
public static void main(String[] args) throws Exception{
  while (true) {
   String s = getString();
   while(true){
    int i = getBit();
    getSubstring(s,i);
   }
   
  }
  
}

public static String getString() throws Exception{//获取键盘输入的字符串的方法
   System.out.println("请输入你需要被截取的字符串:(例如:I love 黑马)");
   BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
   return new String(buf.readLine());
}

public static int getBit() throws Exception{//获取需要截取的位数的方法
   System.out.println("请输入你需要截取的位数:");
   String bit = null;
   boolean flag = true;
   while(flag){
    BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
    bit = buf.readLine();
    byte[] b = bit.getBytes();
    for(int i:b){
     if(!(i>=48 && i<=57)){//判断是否是数字
      System.out.println("输入的数字非法,请重新输入:");
      flag = false;
      break;
     }
    }
       flag = !(flag);
   }
   return Integer.parseInt(bit);
}

//截取子串的方法
public static String getSubstring(String s,int i){
  String sub = null;
  byte[] b = s.getBytes();//把接受进来的字符串转换成字节数组  
  if (i>=b.length) {
   System.out.println("您需要截取的子串长度大于等于源数据长度,所以把源数据全给你:"+s);
   sub = s;
  }
  else{
   int index = i-1;//需要截取的初始位置
   int num = 0;
   //从后往前寻找字节码为负数的数量
   while (index >= 0 && b[index--]<0) {//判断是否是汉字字节
    num++;
   }
   //判断负数字节的奇偶性
   if (num % 2 == 1) {
    sub = new String(b,0,i-1);//舍去最后以为建立子串
   }
   else
    sub = new String(b,0,i);//不舍最后以为建立子串
   
  }
  System.out.println(sub);
  return sub;
}
}

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马