F8265506000300
java  java.lang.String split() 把上面  两个为一组截取为字符串  应该怎么写正则表达式

解决方案 »

  1.   

    split是根据你指定的字符来进行拆分。你这个2个为一组,不要选择这个方法。
      

  2.   

    [code=Java]
    public class T { /**
     * @param args
     */
    public static void main(String[] args) {
    Pattern p = Pattern.compile("\\w{1,2}");
    Matcher m = p.matcher("F82655060003000");//注意, 这里比楼主需求长一个字符.
    while(m.find()) {
    System.out.print(m.group() + ", ");
    }
    System.out.println();

    //上面是使用正则的结果
    //下面是使用自己实现的一个简易split.

    String[] r = split("F82655060003000", 2);
    int i = 0;
    for(; i < r.length; i ++) {
    System.out.print(r[i] + ", ");
    }
    }

    /**
     * @author selfimpr
     * @blog http://blog.csdn.net/lgg201
     * @mail: [email protected]
     * @param target 目标字符串
     * @param width 要处理的宽度
     * @return 处理之后的字符串数组
     * 功能: 将指定字符串按照指定宽度分割  */
    public static String[] split(String target, int width) {
    int length = target.length();
    int result_length = (length + width - 1) / width;
    String[] result = new String[result_length];
    int i = 0;
    while(i < result_length - 1) {
    result[i] = target.substring(i * width, (i + 1) * width);
    i++;
    }
    //打印最后剩余的字符.
    result[i] = target.substring(i * width);
    return result;
    }}
    [code]
      

  3.   


    public class T { /**
     * @param args
     */
    public static void main(String[] args) {
    Pattern p = Pattern.compile("\\w{1,2}");
    Matcher m = p.matcher("F82655060003000");//注意, 这里比楼主需求长一个字符.
    while(m.find()) {
    System.out.print(m.group() + ", ");
    }
    System.out.println();

    //上面是使用正则的结果
    //下面是使用自己实现的一个简易split.

    String[] r = split("F82655060003000", 2);
    int i = 0;
    for(; i < r.length; i ++) {
    System.out.print(r[i] + ", ");
    }
    }

    /**
     * @author selfimpr
     * @blog http://blog.csdn.net/lgg201
     * @mail: [email protected]
     * @param target 目标字符串
     * @param width 要处理的宽度
     * @return 处理之后的字符串数组
     * 功能: 将指定字符串按照指定宽度分割  */
    public static String[] split(String target, int width) {
    int length = target.length();
    int result_length = (length + width - 1) / width;
    String[] result = new String[result_length];
    int i = 0;
    while(i < result_length - 1) {
    result[i] = target.substring(i * width, (i + 1) * width);
    i++;
    }
    //打印最后剩余的字符.
    result[i] = target.substring(i * width);
    return result;
    }}
      

  4.   


    String str = "F8265506000300";
    for(int i=0;i<str.length();i+=2){
    int end = 0;
    if(i+2>str.length()){//这里是处理字符串str里字符个数的奇偶数,以免substring方法出现 java.lang.StringIndexOutOfBoundsException的错误
    end = i+1;
    }else{
    end = i+2;
    }
    String sub = str.substring(i, end);
    System.out.println("sub "+i+" = " + sub);
    }
      

  5.   

    substring和定义自己的split方法都能实现。5楼的,学习了。
      

  6.   

    自己定义的split里用到的依然是substring方法,那又何必多此一举呢?
      

  7.   

    String spStr[] = str.split(",");
    for(int i = 0; i < spStr.length; i++)
    if( spStr[i].indexOf("CN=") >= 0 )
    System.out.println(spStr[i]);
      

  8.   

    或者用charAt(i) ,i%2==0输一次