I have below string which I need to capture last word.
Below is my code but its not getting the last word.
The string at below location is dynamic and is not static string.
String secques1 = driver.findElement(By.xpath("//*[@class='small-12 medium-4 medium-offset-4 columns']/div/div/div/div[1]/div/div[1]/label")).getText();
String lastWord = secques1.substring(secques1.lastIndexOf(" ")+1);`
I have a suggestion string may contains space in it's trailing:
First: try to trim string trailing (string.trim())
second: print the whole text
and check the leading and trailing spaces then print the last word I think this will help you.
The result of getText() is always with blank. And you made a mistake that if the secques1 end with bank the program will throw exception. so ,you need to do like this:
String secques1 = driver.findElement(By.xpath("//*[@class='small-12 medium-4 medium-offset-4 columns']/div/div/div/div[1]/div/div[1]/label")).getText().trim();
add trim()
String lastWord = secques1.substring(secques1.length()-1);
This should work:
String secques1 = " " + driver.findElement(By.xpath("//*[@class='small-12 medium-4 medium-offset-4 columns']/div/div/div/div[1]/div/div[1]/label")).getText();
String lastWord = secques1.substring(secques1.lastIndexOf(" ")+1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.