how to get a substring of size n that repeats

1

I have the following entry:

aaisndiaunwioun    test|test saiudb8iuyb aiwbu diby tab fiubaw palavragrande|palavragrande asibtiubi

How to make the algorithm return test and palavragrande only if the before and after index of the symbol |

I did this but it obviously did not work out

while (s1.indexOf("|") != -1) {
        for (int i = 1; i < 25; i++) {
            for (int j = 1; j < 25; j++) {
                String teste1 = s1.substring(s1.indexOf("|") + 1, s1.indexOf("|") + i);
                String teste2 = s1.substring(s1.indexOf("|") - j, s1.indexOf("|"));
                if (teste1.equals(teste2)) {
                    System.out.println(teste1);
                    char[] s1char = s1.toCharArray();
                    s1char[s1.indexOf("|")] = 'a';
                    s1 = String.valueOf(s1char);
                    System.out.println(s1);
                    System.out.println(s1.indexOf("|"));
                }

            }
    
asked by anonymous 03.05.2018 / 07:52

1 answer

0

You can use indexOf("") to match the entire sentence:

public class Teste {

    private static final String CONST = "aaisndiaunwioun    test|test saiudb8iuyb aiwbu diby tab fiubaw palavragrande|palavragrande asibtiubi";

    public static void main(String[] args) {

        if(CONST.indexOf("test|test") >= 0) {
            System.out.println("test");
        }

        if(CONST.indexOf("palavragrande|palavragrande") >= 0) {
            System.out.println("palavragrande");
        }
    }
}

If the statements "test|test" or "palavragrande|palavragrande" do not exist indexOf("") will return -1 .

EDIT

With the comments below I understood that this was not what I wanted. I have refitted the answer but I will leave the original sample content. Here's the correct answer:

public class Teste {

    private static final String CONST = "aaisndiaunwioun    test|test saiudb8iuyb aiwbu diby tab fiubaw palavragrande|palavragrande asibtiubi";

    public static void main(String[] args) {

        int ultimaPosicaoDoPipe = 0;
        while(CONST.indexOf("|", ultimaPosicaoDoPipe) >= 0) {

            final int posicaoAtualDoPipe = CONST.indexOf("|", ultimaPosicaoDoPipe);
            final String palavraAntesDoPipe = getPalavraAntesDoPipe(ultimaPosicaoDoPipe, posicaoAtualDoPipe);
            final String palavraDepoisDoPipe = getPalavraDepoisDoPipe(posicaoAtualDoPipe);

            if(palavraAntesDoPipe.equals(palavraDepoisDoPipe)) {
                System.out.println(palavraAntesDoPipe);
            }

            ultimaPosicaoDoPipe = posicaoAtualDoPipe + 1;
        }
    }

    private static String getPalavraAntesDoPipe(final int ultimaPosicaoDoPipe, final int posicaoAtualDoPipe) {
        String palavraAntesDoPipe = CONST.substring(ultimaPosicaoDoPipe, posicaoAtualDoPipe);
        palavraAntesDoPipe = palavraAntesDoPipe.substring(palavraAntesDoPipe.lastIndexOf(" ") + 1);
        return palavraAntesDoPipe;
    }

    private static String getPalavraDepoisDoPipe(final int posicaoAtualDoPipe) {
        String palavraDepoisDoPipe = CONST.substring(posicaoAtualDoPipe + 1);
        palavraDepoisDoPipe = palavraDepoisDoPipe.substring(0, palavraDepoisDoPipe.indexOf(" "));
        return palavraDepoisDoPipe;
    }
}

Explanation:

The pipe character ( | ) is looped through method overload indexOf() that receives a second parameter which informs from which index it should search the pipe.

From there, the methods getPalavraAntesDoPipe() and getPalavraDepoisDoPipe() return the words before and after the pipe (as you can imagine: p).

Nothing too complex, just use class methods String .

    
03.05.2018 / 14:13