how to take a substring of size n that repeats

Asked

Viewed 82 times

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 it’s spelled the same before and after of the symbol index |

I did it but it obviously didn’t work

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

            }
  • Is it standard ? There are always spaces before there are test|test ?

  • no, can occur to be any alphanumeric character ex: assatest|test awe

  • If it is assatest|test awe would not be valid ?

  • I need you in case he returns test, in my idea would be a comparison char a char up and down from the symbol | till I find a different char

  • You can’t just do it like this: https://repl.it/repls/DismalUnselfishProfile

  • I don’t think so, when you set an example aaaa|aabaa in theory they are equal up to the 2 a compared from the symbol | back and forth. And the return for me has to be the string q was verified in the above case aa

Show 1 more comment

1 answer

0

You can use the 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 sentences "test|test" or "palavragrande|palavragrande" there is no indexOf("") will return -1.


EDIT

With the comments below I understood that this is not what I wanted.
I remade the answer but I’ll leave the original content example.
Follow 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 sought in loop through the overload of the method indexOf() which receives a second parameter that tells from which index it should look for the pipe.

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

Nothing too complex, just using class methods String.

  • I think I expressed myself wrong, the words test and palaver were just examples, I imagine random words but following the pattern of having the symbol | in the middle

  • Do you know what words to search for? Or will you search for the pipe (|) and find the words before and after?

  • I’ll look for them by the pipe | they will be random

  • @unkbr updated the answer. Could you see if it now answers, please?! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.