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
.
Is it standard ? There are always spaces before there are
test|test
?– NoobSaibot
no, can occur to be any alphanumeric character ex: assatest|test awe
– unkbr
If it is
assatest|test awe
would not be valid ?– NoobSaibot
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– unkbr
You can’t just do it like this: https://repl.it/repls/DismalUnselfishProfile
– NoobSaibot
I don’t think so, when you set an example
aaaa|aabaa
in theory they are equal up to the 2a
compared from the symbol|
back and forth. And the return for me has to be the string q was verified in the above caseaa
– unkbr