自定义字符串在Java分裂法 [英] Custom String split method in java

查看:236
本文介绍了自定义字符串在Java分裂法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个自定义字符串分割方法,我迷失在循环。我会后我的code到目前为止,希望有人能告诉我在哪里,我错了。我知道有更好的方法可以做到这一点,但我只是想知道为什么我似乎无法得到的for循环正确读取。基本上,我希望能够在一次设置多个分隔符。所以,我只是填充分隔符的数组,并试图每个字符在满弦用分隔符阵列的每个条目进行比较。如果它不是一个分隔符,将它添加到字符串,如果是,它打破了循环,并把它创建的字符串,并将其添加到字符串数组中的第一项。这应该继续下去,直到字符数组已完成。

下面是我的code:

 的String [] = charArray新的String [s.length()];
    的String []字符串数组=新的String [s.length()];
    的String [] = delimArray新的String [regex.length()];    //装满分隔符值数组
    的for(int i = 0; I< delimArray.length;我++){
        delimArray [I] = Character.toString(regex.charAt(ⅰ));
    }    //按字符填写了所有值数组中的字符串
    的for(int i = 0; I< charArray.length;我++){
        charArray [I] = Character.toString(s.charAt(ⅰ));
    }    的for(int i = 0; I< stringArray.length;我++){
        字符串S1 =;
        对于(INT K = 0; K< charArray.length; k ++){
            对于(INT J = 0; J< delimArray.length; J ++){
                如果(charArray [K]!= delimArray [J]){
                    S1 = S1 + charArray [K];
                }否则如果(charArray [K] == delimArray [J]){
                    字符串数组[I + 1] = delimArray [J]。
                    打破;
                }
            }
            S1 = S1 + charArray [K];
        }
        字符串数组[我] = S1;
    }


解决方案

试试这个: -

 字符串正则表达式=;
        的String [] = charArray新的String [s.length()];
        的String []字符串数组=新的String [s.length()];
        的char [] = delimArray新的char [regex.length()];     //不需要,你可以直接使用regex.charAt(I)
        //装满分隔符值数组
        / *的for(int i = 0; I< delimArray.length;我++){
            delimArray [I] = regex.charAt(ⅰ);
        }
* /
        //不需要,你可以直接使用s.charAt(I)
        //按字符填写了所有值数组中的字符串
        / *的for(int i = 0; I< charArray.length;我++){
            charArray [I] = Character.toString(s.charAt(ⅰ));
        } * /            INT I = 0;
            //外环
            外:
            对于(INT K = 0; K< s.length(); K ++){
                //内环
                内:
                对于(INT J = 0; J< delimArray.length; J ++){
                    //如果检查每个分隔符和收益字符串中的当前字符,直到它会检查所有的分隔符
                    如果(s.charAt(K)!= regex.charAt(J)){
                        继续内;
                    }其他{
                        //如果当前字符是定界符然后DONOT其添加到最终的字符串数组结果
                        我++;
                        继续外;
                    }
                }
                //如果这是为了避免空被附加到字符串
                如果(字符串数组[我] == NULL){
                    字符串数组[I] =;
                }
                //添加字符(因为它不是一个分隔符)到当前索引i
                字符串数组[I] + = Character.toString(s.charAt(K));
            }
        //}

已编辑答案: -

 一个String =#AB 12#45-3
        的System.out.println(s.matches(\\\\ D +));        字符串的正则表达式= - #;
        的String [] = charArray新的String [s.length()];
        的String []字符串数组=新的String [s.length()];
        的char [] = delimArray新的char [regex.length()];     //不需要,你可以直接使用regex.charAt(I)
        //装满分隔符值数组
        / *的for(int i = 0; I< delimArray.length;我++){
            delimArray [I] = regex.charAt(ⅰ);
        }
* /
        //不需要,你可以直接使用s.charAt(I)
        //按字符填写了所有值数组中的字符串
        / *的for(int i = 0; I< charArray.length;我++){
            charArray [I] = Character.toString(s.charAt(ⅰ));
        } * /            INT I = 0;
            //外环
            外:
            对于(INT K = 0; K< s.length(); K ++){
                //内环
                内:
                对于(INT J = 0; J< delimArray.length; J ++){
                    //如果检查每个分隔符和收益字符串中的当前字符,直到它会检查所有的分隔符
                    如果(s.charAt(K)!= regex.charAt(J)){
                        继续内;
                    }其他{
                        到达// else块当任何字符串中的字符是分隔符
                        //检查当前数组位置为空(这意味着什么是迄今为止分配),并且移动到下一个位置,只有当它不为空
                        如果(字符串数组[I]!= NULL){
                            我++;
                        }
                        //指定分隔符在输出数组中
                        字符串数组[我] = Character.toString(s.charAt(K));
                        //递增到阵列中的下一个位置
                        我++;
                        继续外;
                    }
                }
                //如果这是为了避免空被附加到字符串
                如果(字符串数组[我] == NULL){
                    字符串数组[I] =;
                }
                //添加字符(因为它不是一个分隔符)到当前索引i
                字符串数组[I] + = Character.toString(s.charAt(K));
            }
        //}
        / *
         *为了避免最终分裂阵列,我们需要在空值初始化一个多个阵列
         *和只分配有效值最终分裂阵列
         * /
        的String [] = splitStrArr新的String [I + 1];
        INT m为0;
        做{
            splitStrArr [M] =字符串数组[M]。
            M +;
        }而(M< = 1);

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:-

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

这篇关于自定义字符串在Java分裂法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆