正则表达式怎么把一个String截取为长20个字符的字符串数组?比如"aaaa..."(40个)截取为长度为20个字符的两个字符串?

解决方案 »

  1.   

    String.substring()不就可以吗?用正则干什么?
      

  2.   


    String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567";
        Matcher matcher =Pattern.compile(regex).matcher(str);
        while(matcher.find()) System.out.println(Arrays.toString(  matcher.group().toCharArray()  ));
        /*
            [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t]
    [u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N]
            [O, P, Q, R, S, T, U, V, W, X, Y, Z, 0, 1, 2, 3, 4, 5, 6, 7]
         */java传参的方式: A,只有值传递,B,既有值传递又有引用传递
    楼主选A还是B?
    上次我回复了你 现在打听一下楼主理解了么
      

  3.   

      regex = "\\w{20}"  
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567";
        Matcher matcher =Pattern.compile(regex).matcher(str);
        while(matcher.find()) System.out.println(Arrays.toString(  matcher.group().toCharArray()  ));
        /*
            [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t]
    [u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N]
            [O, P, Q, R, S, T, U, V, W, X, Y, Z, 0, 1, 2, 3, 4, 5, 6, 7]
         */
      

  4.   


    regex = "\\w{20}"  这个可以试试看