【问题标题】:Custom String split method in javajava中的自定义字符串拆分方法
【发布时间】:2014-09-10 03:51:36
【问题描述】:

我正在尝试实现自定义拆分字符串方法,但我迷失在 for 循环中。到目前为止,我将发布我的代码,希望有人能告诉我哪里出错了。我知道有更好的方法可以做到这一点,但我只是想知道为什么我似乎无法正确读取 for 循环。基本上我希望能够一次设置多个分隔符。因此,我填充了一个仅包含分隔符的数组,并尝试将完整字符串中的每个字符与分隔符数组中的每个条目进行比较。如果它不是分隔符,则将其添加到字符串中,如果是,则中断循环并获取它创建的字符串并将其添加到字符串数组的第一个条目中。这应该一直持续到字符数组完成为止。

这是我的代码:

    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;
    }

【问题讨论】:

  • 您的代码到底有什么不正确的地方?
  • 你提供了什么输入,你期望什么输出?
  • 方向如下: split(\ab#12#453", \#") 在 String 数组中返回 ab, #, 12, #, 453 。 split(\a?b?gf#e", [?#]") 在字符串数组中返回 a, ?, b, ?, gf, #, e

标签: java arrays string loops split


【解决方案1】:

试试这个-

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()]);
    }

【讨论】:

    【解决方案2】:

    试试这个:-

            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));
                }
            //}
    

    编辑答案:-

    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);              
    

    【讨论】:

    • 这是一个很大的变化。我从未考虑过参考点在整个循环中移动。到目前为止,循环正在工作,但不幸的是我仍然遇到一些问题。这是到目前为止的输出:ab, 12, 453, null, null, null, null, null, null 我最后仍然得到空值,而且我也缺少分隔符。输出应该是 ab, #, 12, #, 453
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多