输入一个字符串和一个数字,如"我ABC" 4,返回结果是按输入的数字截取的字符串,结果为:我AB
再如“我ABC你DEF",6,结果为”我ABC“,'你'字只能取半个,但是不让这半个显示。直显示"我ABC"就好

解决方案 »

  1.   

    http://topic.csdn.net/u/20070709/10/25126567-81f7-46f0-a475-08c20303bf34.html
      

  2.   

    public static String jiequ(String s,int len){
    String res = "";
    char c[] = s.toCharArray();
    int i = 0;//循环的次数
    int m = 0;//取得字符的位置
    while(i < len){
    if((int)c[m]<256){
    res = res + c[m];
    m++;
    i++;
    }else{
    i = i+2;
    if(i<len){
    res = res + c[m];
    m++;
    }else{
    break;
    }
    }
    }

    return res;
    }
      

  3.   

    public String subString(String str, int subBytes) {
    int bytes = 0; // 用来存储字符串的总字节数
    for (int i = 0; i < str.length(); i++) {
    if (bytes == subBytes) {
    return str.substring(0, i);
    }
    char c = str.charAt(i);
    if (c < 256) {
    bytes += 1; // 英文字符的字节数看作1
    } else {
    bytes += 2; // 中文字符的字节数看作2
    if(bytes - subBytes == 1){
    return str.substring(0, i);
    }
    }
    }
    return str;
    }
      

  4.   

    汉字是两个字节。字母是一个字节用
       
    System.out.println("a".getBytes().length) ;  
       System.out.println("有".getBytes().length) ;  可以解决你的问题
      

  5.   

    public class JieQu { public static void main(String[] args) {
    System.out.println(jieQu("我ABC你DEF", 6));
    } public static String jieQu(String s, int n) {
    int k;
    int m = 0;
    byte a[] = s.getBytes();
    for (int i = 0; i < n; i++) {
    if (a[i] < 0) {
    m++;
    }
    }
    if (m % 2 == 0) {
    k = n;
    } else {
    k = n -1;
    }
    return new String(a, 0, k);
    }}刚写的,测了,可以~~说实话,不相信这是面试题,太容易了~~
    睡觉去了~~
      

  6.   

    我也来凑热闹, 哈哈:public class Test {
    public static void main(String[] args) {
    String str = "我是Ni你";
    String result = "";
    int count = 3;
    int index = 0;
    for (int i = 0; i < str.length(); ++i) {
    int first = (str.charAt(i) >> 8) & 0xFF;

    if (first > 0) {
    if (index + 2 <= count) {
    result += str.charAt(i);
    index += 2;
    } else {
    break;
    }
    } else {
    result += str.charAt(i);
    ++index;
    }
    }

    System.out.println(result);
    }
    }
      

  7.   

    下面是我的代码,测试结果的对的;
    public class ShowString { /**
     * @param args
     */
    public static void main(String[] args) {

    String str = "我abc你bcd";
    System.out.println("字符串:" + str + " 参数:4\n" + getString(str, 4));
    System.out.println("字符串:" + str + " 参数:6\n" + getString(str, 6));

    }//end main

    private static String getString(String str, int index) {
    int i = 0; 
    int count = -1;
    int c = str.charAt(i);

    while (c != '\n') {
    if (c > 255) { //表示只有低8为有值
    count += 2;
    } else { //高8位也有值
    count++;
    }

    if (count >= index) { //当计数器等于下标时,截断
    return str.substring(0, i);
    }

    c = str.charAt(++i);
    }
    return null;
    }//end getString}测试结果:
    字符串:我abc你bcd 参数:4
    我ab
    字符串:我abc你bcd 参数:6
    我abc
      

  8.   

    前面有点乱,冲贴了一下/**
     @author 王平
     2010-2-1
     */
    public class ShowString { /**
     * @param args
     */
    public static void main(String[] args) {

    String str = "我abc你bcd";
    System.out.println("字符串:" + str + " 参数:4\n" + getString(str, 4));
    System.out.println("字符串:" + str + " 参数:6\n" + getString(str, 6));

    }//end main

    private static String getString(String str, int index) {
    int i = 0; 
    int count = -1;
    int c = str.charAt(i);

    while (c != '\n') {
    if (c > 255) { //表示只有低8为有值
    count += 2;
    } else { //高8位也有值
    count++;
    }

    if (count >= index) { //当计数器等于下标时,截断
    return str.substring(0, i);
    }

    c = str.charAt(++i);
    }
    return null;
    }//end getString}
      

  9.   


    /**
     @author 王平
     2010-2-1
     */
    public class ShowString { /**
     * @param args
     */
    public static void main(String[] args) {

    String str = "我abc你bcd";
    System.out.println("字符串:" + str + " 参数:4\n" + getString(str, 4));
    System.out.println("字符串:" + str + " 参数:6\n" + getString(str, 6));

    }//end main

    private static String getString(String str, int index) {
    int i = 0; 
    int count = -1;
    int c = str.charAt(i);

    while (c != '\n') {
    if (c > 255) { //表示只有低8为有值
    count += 2;
    } else { //高8位也有值
    count++;
    }

    if (count >= index) { //当计数器等于下标时,截断
    return str.substring(0, i);
    }

    c = str.charAt(++i);
    }
    return null;
    }//end getString}
      

  10.   

    刚才字符串判断符应该为c!='\n',再重发一下
    public class ShowString { /**
     * @param args
     */
    public static void main(String[] args) {

    String str = "我abc你bcd";
    System.out.println("字符串:" + str + " 参数:4\n" + getString(str, 9));
    System.out.println("字符串:" + str + " 参数:6\n" + getString(str, 6));

    }//end main

    private static String getString(String str, int index) {
    int i = 0; 
    int count = -1;
    int c = str.charAt(i);

    while (c != '\0') {      
    if (c > 255) { //表示只有低8为有值
    count += 2;
    } else { //高8位也有值
    count++;
    }

    if (count >= index) { //当计数器等于下标时,截断
    return str.substring(0, i);
    }

    c = str.charAt(++i);
    }
    return null;
    }//end getString}
      

  11.   

    public static String formatStringInByteLength(String str, int byteLength){
    byte[] data = str.getBytes();
    byteLength = byteLength > data.length ? data.length : byteLength;
    for(int i = 1; ;i++){
    if(i == byteLength + 1 || data[byteLength - i] > 0){
    if(i % 2 == 0){
    byteLength --;
    }
    break;
    }
    }
    return new String(data, 0, byteLength);
    }
      

  12.   

    上面格式乱了,重发一次 public static String formatStringInByteLength(String str, int byteLength){
    byte[] data = str.getBytes();
    byteLength = byteLength > data.length ? data.length : byteLength;
    for(int i = 1; ;i++){
    if(i == byteLength + 1 || data[byteLength - i] > 0){
    if(i % 2 == 0){
    byteLength --;
    }
    break;
    }
    }
    return new String(data, 0, byteLength);
    }
      

  13.   

    ciltr,原来字符串中还可以转化为纯字节数组,受教了
      

  14.   

    上面那个是处理GBK编码的,再上一个处理utf-8编码的。 public static void main(String[] args) throws IOException {
    String s = "我abc你";//在utf-8编码中占9个字节
    String res = formatStringInByteLength_utf8(s, 8);
    System.out.println(res);

    }
    public static String formatStringInByteLength_utf8(String str, int byteLength) throws UnsupportedEncodingException{
    byte[] data = str.getBytes("UTF-8");
    for(byte b : data){
    System.out.println(String.format("%x", b));
    }
    byteLength = byteLength > data.length ? data.length : byteLength;
    int count = 0;
    while((data[byteLength - 1] & 192) == 128){
    byteLength --;
    count ++;
    }
    if(count != 0){
    if(count == 1 && (data[byteLength - 1] & 224) == 192
    || count == 2 && (data[byteLength - 1] & 224) == 224){
    byteLength += count;
    }
    else{
    byteLength --;
    }
    }
    else if((data[byteLength - 1] & 128) == 128){
    byteLength --;
    }
    return new String(data, 0, byteLength, "UTF-8");
    }