JAVA / SELENIUM ... Show only a section of my String

0

I'm working on Selenium and when I do the GetText message I get something like this:

  

String message = driver.findElement (By.Xpath ("IdMessage")). getText ();

And in the message variable the following is captured:

Mensaje aceptado correctamente

then I need to take the word "correctly" only, but I will not do it with the substring action that has java because sometimes they add or remove things from that message, but by user rule always the word should remain correctly in the message.

I am doing a conditional if to say that if my variable message brings the word correctly , I will do certain actions promptly.

Question: Does Java have any way to obtain from a String string only the punctual text that is required?

    
asked by BlackWidow 17.05.2018 в 17:36
source

2 answers

0

To do what you want you must use the contains() method of the class String , which lets you know if one string contains another.

String msg = "Mensaje aceptado correctamente";
if (msg.contains("correctamente")) {// En este caso será true
    // Código
} else {
    // Código
}
    
answered by 17.05.2018 в 19:31
0

You could also store that String in array of strings with the split(); method and get the position of the word correctly .

String mensaje = "Mensaje aceptado correctamente";    
String[] aS = mensaje.split(" ");
System.out.println("Extrayendo "+aS[2]);

And you already decide what to do with it.

    
answered by 17.05.2018 в 20:43