简体   繁体   中英

Custom String split method in java

I am trying to implement a custom split string method and I am getting lost in for loops. I will post my code so far and hopefully someone can tell me where I am going wrong. I know there are better ways to do this, but I am just wondering why I can't seem to get the for loops to read properly. Basically I want to be able to set multiple delimiters at once. So I was populating a an array of just the delimiters and trying to compare each character in the full string with each entry in the delimiter Array. If it was not a delimiter, it added it to a string, if it was, it broke off the loop and took the string it created and added it to the first entry in the string array. This should continue until the character array was done.

Here is my code:

    String[] charArray = new String[s.length()];
    String[] stringArray = new String[s.length()];
    String[] delimArray = new String[regex.length()];

    // Fill array with delimiter values
    for (int i=0; i < delimArray.length; i++) {
        delimArray[i] = Character.toString(regex.charAt(i));
    }

    // Fill array with all values in string by character
    for (int i=0; i < charArray.length; i++) {
        charArray[i] = Character.toString(s.charAt(i));
    }

    for (int i=0; i < stringArray.length; i++) {
        String s1 = "";
        for (int k=0; k < charArray.length; k++) {
            for (int j=0; j < delimArray.length; j++) {
                if  (charArray[k] != delimArray[j]) {
                    s1 = s1 + charArray[k];
                } else if (charArray[k] == delimArray[j]) {
                    stringArray[i+1] = delimArray[j];
                    break;
                }
            }
            s1 = s1 + charArray[k];
        }
        stringArray[i] = s1;
    }

Try this-

public static String[] split(String string, String delem) {
        ArrayList<String> list = new ArrayList<String>();
        char[] charArr = string.toCharArray();
        char[] delemArr = delem.toCharArray();
        int counter = 0;
        for (int i = 0; i < charArr.length; i++) {
            int k = 0;
            for (int j = 0; j < delemArr.length; j++) {
                if (charArr[i+j] == delemArr[j]) {
                    k++;
                } else {
                    break;
                }
            }
            if (k == delemArr.length) {
                String s = "";
                while (counter < i ) {
                    s += charArr[counter];
                    counter++;
                }
                counter = i = i + k;
                list.add(s);
                //System.out.println(" k = "+k+" i= "+i);
            }
        }
        String s = "";
        if (counter < charArr.length) {
            while (counter < charArr.length) {
                s += charArr[counter];
                counter++;
            }
            list.add(s);
        }
        return list.toArray(new String[list.size()]);
    }

Try this:-

        String regex = "";
        String[] charArray = new String[s.length()];
        String[] stringArray = new String[s.length()];
        char[] delimArray = new char[regex.length()];

     // Not required as you can directly use regex.charAt(i)
        // Fill array with delimiter values
        /*for (int i=0; i < delimArray.length; i++) {
            delimArray[i] = regex.charAt(i);
        }
*/
        // Not required as you can directly use s.charAt(i)
        // Fill array with all values in string by character
        /*for (int i=0; i < charArray.length; i++) {
            charArray[i] = Character.toString(s.charAt(i));
        }*/

            int i = 0;
            // Outer loop
            outer:
            for (int k=0; k < s.length(); k++) {
                // Inner loop
                inner :
                for (int j=0; j < delimArray.length; j++) {
                    // The if checks the current character in the string with each delimiter character and proceeds till it checks all the delimiters 
                    if  (s.charAt(k) != regex.charAt(j)) {  
                        continue inner;                     
                    } else {
                        // If the current character is a delimiter then donot add it to the final string array result
                        i++;
                        continue outer;
                    }
                }
                // This if is to avoid null being appended to the string
                if(stringArray[i] == null) {
                    stringArray[i] = "";
                }
                // Add the character(since it is not a delimiter) to the current index i 
                stringArray[i] += Character.toString(s.charAt(k));
            }
        //}

EDITED ANSWER:-

String s = "ab#12#45-3";
        System.out.println(s.matches("\\d+"));

        String regex = "-#";
        String[] charArray = new String[s.length()];
        String[] stringArray = new String[s.length()];
        char[] delimArray = new char[regex.length()];

     // Not required as you can directly use regex.charAt(i)
        // Fill array with delimiter values
        /*for (int i=0; i < delimArray.length; i++) {
            delimArray[i] = regex.charAt(i);
        }
*/
        // Not required as you can directly use s.charAt(i)
        // Fill array with all values in string by character
        /*for (int i=0; i < charArray.length; i++) {
            charArray[i] = Character.toString(s.charAt(i));
        }*/

            int i = 0;
            // Outer loop
            outer:
            for (int k=0; k < s.length(); k++) {
                // Inner loop
                inner :
                for (int j=0; j < delimArray.length; j++) {
                    // The if checks the current character in the string with each delimiter character and proceeds till it checks all the delimiters 
                    if  (s.charAt(k) != regex.charAt(j)) {  
                        continue inner;                     
                    } else {
                        // Else block is reached when any of the character in the string is a delimiter
                        // Check if the current array position is null(which means nothing is assigned so far) and move to the next position only if it is not null
                        if(stringArray[i] != null) {
                            i++;
                        }
                        // Assign the delimiter in the output array
                        stringArray[i] = Character.toString(s.charAt(k));
                        // Increment to the next position in the array
                        i++;
                        continue outer;
                    }
                }
                // This if is to avoid null being appended to the string
                if(stringArray[i] == null) {
                    stringArray[i] = "";
                }
                // Add the character(since it is not a delimiter) to the current index i 
                stringArray[i] += Character.toString(s.charAt(k));
            }
        //}
        /*
         * To avoid nulls in the final split array we need to initialize one more array
         * and assign only valid values to the final split array    
         */
        String[] splitStrArr = new String[i+1];
        int m = 0;
        do {
            splitStrArr[m] = stringArray[m];
            m++;
        } while (m <= i);              

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM