搜索了很多都是实现substring(int beginIndex)功能的,不知道substring(int beginIndex,int endIndex)要怎么实现?

解决方案 »

  1.   

    substring 方法返回位于 String 对象中指定位置的子字符串。strVariable.substring(start, end)
    "String Literal".substring(start, end)
    参数start指明子字符串的起始位置,该索引从 0 开始起算。end指明子字符串的结束位置,该索引从 0 开始起算。
    说明substring 方法将返回一个包含从 start 到最后(不包含 end )的子字符串的字符串。substring 方法使用 start 和 end 两者中的较小值作为子字符串的起始点。例如, strvar.substring(0, 3) 和 strvar.substring(3, 0) 将返回相同的子字符串。如果 start 或 end 为 NaN 或者负数,那么将其替换为0。子字符串的长度等于 start 和 end 之差的绝对值。例如,在 strvar.substring(0, 3) 和 strvar.substring(3, 0) 返回的子字符串的的长度是 3。
    示例下面的示例演示了 substring 方法的用法。function SubstringDemo(){
       var ss;                         // 声明变量。
       var s = "The rain in Spain falls mainly in the plain..";
       ss = s.substring(12, 17);   // 取子字符串。
       return(ss);                     // 返回子字符串。
    }
      

  2.   

    谢谢LS,可能我没说清楚,我要的是处理中英文混合字符串截取,直接用substring处理不行
      

  3.   

    怎么不行?绝对可以
    import java.util.*;
    class Test
    {
    public static void main(String args[])
    {
    String str="ab我是c积极";
    String sub=str.substring(1,5);
    System.out.println(sub);
    }
    }
      

  4.   

    但是我要的不是这个结果,汉字算两个字符,英文算一个字符,
    比如
    String str="ab我是c积极";
    String sub=str.substring(1,6);
    System.out.println(sub);
    结果要的是:
    b我是
    而不是:
    b我是c积现在处理定长数据时候碰到了问题,有什么解决办法?
      

  5.   

    那如果是“我市”两个字,你要取substring(0,1);怎么办?
    把一个汉字算两个字符,你得取出汉字的一半,那取出的是什么?乱玛
      

  6.   

    java处理字符串时候是变成一个字符数组,然后汉字和英文都是一个字符而已
      

  7.   

    思路:字符串中包含有汉字、字母、数字和其它特殊字符。其中汉字存放2字节,且每个字节的ASCII值均小于0,其它字符的ASCII均大于等于0。当前定位为index。先判断此位置字符是否为字母,如果是则很好处理;关键问题在于如果是汉字,则从此汉字向前后查找,直到遇上第一个字母,对此字母到汉字的距离对2求余数,若为0,则index是汉字的后一个字节,若不为0则index为汉字的前一个字符。
    代码如下:public class Test {
        public static void main(String[] args){
            String str = "A中b国日日";
            System.out.println(subString(str,5,10));
        }
    static String subString(String str,int beginIndex,int endIndex){
        byte[] bt = str.getBytes();
        int len = bt.length;
        int tempBegin = beginIndex - 1;
        int tempEnd = endIndex - 1;
        if (bt[tempBegin] >= 0 && bt[tempEnd] >= 0){
            return new String(bt,tempBegin,endIndex - beginIndex + 1);
        }
        else if(bt[tempBegin] >= 0 && bt[tempEnd] < 0){
            tempEnd = findPosition(str,tempEnd);
            int end = Math.abs(endIndex - 1 - tempEnd) % 2;
            if (end != 0){
                return new String(bt,tempBegin,endIndex - beginIndex);
            }else{
                return new String(bt,tempBegin,endIndex - beginIndex + 1);
            }
        }
        else if(bt[tempBegin] < 0 && bt[tempEnd] >= 0){
            tempBegin = findPosition(str,tempBegin);
            int begin = Math.abs(tempBegin - beginIndex + 1) % 2;
            if (begin != 0){
                return new String(bt,beginIndex,endIndex - beginIndex);
            }else{
                return new String(bt,beginIndex - 1,endIndex - beginIndex + 1);
            }
        }
        else if(bt[tempBegin] < 0 && bt[tempEnd] < 0){
            tempBegin = findPosition(str,tempBegin);
            if (tempBegin == -1){
                return null;//字符串全部为汉字,我没做处理,很简单。
            }
            tempEnd = findPosition(str,tempEnd);
            int begin = Math.abs(tempBegin - beginIndex + 1) % 2;
            int end = Math.abs(endIndex - 1 - tempEnd) % 2;
            if (begin != 0 && end != 0){
                return new String(bt,beginIndex - 1,endIndex - beginIndex + 1 - 1);
            }else if(begin != 0 && end == 0){
                return new String(bt,beginIndex - 1,endIndex - beginIndex + 1);
            }else if(begin == 0 && end != 0){
                return new String(bt,beginIndex,endIndex - beginIndex - 1);
            }else if(begin == 0 && end == 0){
                return new String(bt,beginIndex,endIndex - beginIndex);
            }
        }
        else{
             return null;
        }
        return null;
    }
    static int findPosition(String str,int index){//查找从index开始,遇到第一个字母的位置。
        byte[] bt = str.getBytes();
        int toBegin = index;
        int toEnd = index;
        while(true){
            if (toBegin == -1) break;
            if(bt[toBegin] >= 0) return toBegin;
            else toBegin--;
        }
        while(true){
            if (toEnd == bt.length) break; 
            if(bt[toEnd] == 0) return toEnd;
            else toEnd++;
        }
        return -1;//字符串中全部为汉字
    }
    }
    日文2000系统下 测试过,基本没有问题。
      

  8.   

    以下代码,忘了哪里来的转贴了。第一个参数,传入的是要截的中英文字符串,第二个参数,要截取的长度。
    如:subString("2我人a",4);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;
    }