public String[] split(String regex,
                      int limit)
那个limit有什么 作用 我不懂

解决方案 »

  1.   

    限制regex被匹配的次数,当然也就影响到了返回数组的大小。
      

  2.   

    The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. 
      

  3.   

    java中split使用简介:------整理:java_流子 Mar.08 2006-------- 
    java.lang.string.split
    split 方法
    将一个字符串分割为子字符串,然后将结果作为字符串数组返回。
    stringObj.split([separator,[limit]])
    参数
    stringObj 
    必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。
    separator 
    可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽略该选项,返回包含整个字符串的单一元素数组。 
    limit
    可选项。该值用来限制返回数组中的元素个数。说明
    split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解。separator 不作为任何数组元素的部分返回。示例1:
    public > public static String[] ss=new String[20];
    public SplitDemo() {
       
           String s = "The rain in Spain falls mainly in the plain.";
           // 在每个空格字符处进行分解。
           ss = s.split(" ");     

    public static void main(String[] args) {    SplitDemo demo=new SplitDemo();
        for(int i=0;i<ss.length;i++)
        System.out.println(ss[i]);
    }}程序结果:
    The
    rain
    in
    Spain
    falls
    mainly
    in
    the
    plain.
    示例2:
    public > public static String[] ss=new String[20];
    public SplitDemo() {
       
           String s = "The rain in Spain falls mainly in the plain.";
           // 在每个空格字符处进行分解。
           ss = s.split(" ",2);    

    public static void main(String[] args) {
        SplitDemo demo=new SplitDemo();
        for(int i=0;i<ss.length;i++)
        System.out.println(ss[i]);
    }}
    程序结果:
    The
    rain in Spain falls mainly in the plain.示例3:
    public > public static String[] ss=new String[20];
    public SplitDemo() {
       
           String s = "The rain in Spain falls mainly in the plain.";
           // 在每个空格字符处进行分解。
           ss = s.split(" ",20);    

    public static void main(String[] args) {
        SplitDemo demo=new SplitDemo();
        for(int i=0;i<ss.length;i++)
        System.out.println(ss[i]);
    }}
    程序结果:
    The
    rain
    in
    Spain
    falls
    mainly
    in
    the
    plain.
    public static void main(string[] args) {
    string value = "192.168.128.33";
    string[] names = value.split(".");
    for (int i = 0; i < names.length; i++) {
    system.out.println(names[i]);
    }}
    运行结果: 对,没看错!没有任何输出! 
    让我们来看看 split 方法的方法签名吧: 
    public string[] split(string regex) 
    这里的参数的名称是 regex ,也就是 regular expression (正则表达式)。这个参数并不是一个简单的分割用的字符,而是一个正则表达式,看了 split 方法的实现代码就更坚定了我们的信心: 
    public string[] split(string regex, int limit) { 
    return pattern.compile(regex).split(this, limit); 

    split 的实现直接调用的 matcher 类的 split 的方法。读者已经知道,“ . ”在正则表达式中有特殊的含义,因此我们使用的时候必须进行转义。 
    public static void main(string[] args) {
    string value = "192.168.128.33";
    string[] names = value.split("\\.");
    for (int i = 0; i < names.length; i++) {
    system.out.println(names[i]);
    }}
    输出结果:
    192
    168
    128
    33 如果用竖线“|”分隔的话,将出现不可得到的结果,必须改为“\\|”
      

  4.   

            
            String str = "1,2,";
            String[] result = str.split(","); // 结果是result[0] = "1";result[1]="2" 
            String[] result = str.split(",",2); // 结果是result[0] = "1";result[1]="2," 我曾经有碰到过需要拿,最后个的空串的只能加Limit参数了