Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Vector;

public class Main {
    public static String[] split(final String s, final char c, final boolean dblquotes, final int max) {
        int j = 0;
        final Vector<String> vector = new Vector<String>();

        // add first max-1 components
        int num = 0;
        int i = 0;
        String ss = null;
        int k1;
        int k2;
        for (i = 0; num != max - 1; i = j + 1) {
            k1 = -1;
            k2 = -1;
            j = s.indexOf(c, i);
            if (dblquotes) {
                // should have k1=0
                k1 = s.indexOf('"', i);
                // quote found and before delimiter
                if (k1 >= 0 && k1 < j) {
                    // next quote
                    k2 = s.indexOf('"', k1 + 1);
                    if (k2 >= 0) {
                        // recompute next delimiter - should have j=k2+1
                        j = s.indexOf(c, k2 + 1);
                    }
                }
            }
            if (j >= 0) {
                if (dblquotes && k1 >= 0 && k2 >= 0) {
                    ss = s.substring(k1 + 1, k2);
                } else {
                    ss = s.substring(i, j);
                }
                vector.addElement(ss);
                num++;
            } else {
                if (dblquotes && k1 >= 0 && k2 >= 0) {
                    ss = s.substring(k1 + 1, k2);
                } else {
                    ss = s.substring(i);
                }
                vector.addElement(ss);
                num++;
                break;
            }
        }

        // add the max-th component
        k1 = -1;
        k2 = -1;
        if (max != 0 && j >= 0) {
            if (dblquotes) {
                k1 = s.indexOf('"', i);
                // quote found and before delimiter
                if (k1 >= 0) {
                    // next quote
                    k2 = s.indexOf('"', k1 + 1);
                }
            }
            if (dblquotes && k1 >= 0 && k2 >= 0) {
                ss = s.substring(k1 + 1, k2);
            } else {
                ss = s.substring(i);
            }
            vector.addElement(ss);
            num++;
        }

        // convert to array
        final String as[] = new String[num];
        vector.copyInto(as);

        // return the array
        return as;
    }
}