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
.