Quality Testing

Quality is delighting customers

How to print duplicate words in a sentence using Selenium WebDriver?

Print duplicate words in a sentence using Webdriver

Views: 119

Reply to This

Replies to This Discussion

Hi Madhavi,

You can use the below method to find the duplicate words in a sentence

Find the sentence using webdriver.findElement(By.xpath(locator))and use the below method by passing string as parameter.


/** * Method to find duplicate words in a Sentence or String */
public Set<String> duplicateWordsInSentence(String sentence) {
if (sentence == null || sentence.isEmpty()) {
return Collections.emptySet();
}
Set<String> duplicates = new HashSet<String>();
String[] words = sentence.split("\\s+");
Set<String> set = new HashSet<String>();
for (String word : words) {
if (!set.add(word)) {
duplicates.add(word);

}
}
return duplicates;
}

Thanks

Thanks Anand. It works fine.

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2018   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service