Java 从webelement列表中删除逗号和分号
我不熟悉web自动化和使用selenium+jbehave 从上面的输出我需要消除逗号和冒号,有人能帮我吗 我的预期产出是Java 从webelement列表中删除逗号和分号,java,selenium,collections,selenium-webdriver,jbehave,Java,Selenium,Collections,Selenium Webdriver,Jbehave,我不熟悉web自动化和使用selenium+jbehave 从上面的输出我需要消除逗号和冒号,有人能帮我吗 我的预期产出是 Labels:[Rate:, , , Method:, , ] 谈到“逗号”,它们在输出中,因为您已将空字符串添加到列表中。您可以通过使用isEmpty()方法检查空字符串来避免这种情况: Labels:[Rate, Method] for (WebElement temp : lableList) { String text = temp.getText().
Labels:[Rate:, , , Method:, , ]
谈到“逗号”,它们在输出中,因为您已将空字符串添加到列表中。您可以通过使用
isEmpty()
方法检查空字符串来避免这种情况:
Labels:[Rate, Method]
for (WebElement temp : lableList) {
String text = temp.getText().trim(); // trim() removes leading and trailing whitespaces
if (!str.isEmpty())
{
labelOne.add(text);
}
}
那么分号呢(如果只有分号困扰您),您可以轻松地使用.replaceAll()
方法:
Labels:[Rate, Method]
for (WebElement temp : lableList) {
String text = temp.getText().trim(); // trim() removes leading and trailing whitespaces
if (!str.isEmpty())
{
labelOne.add(text);
}
}
如果要删除多种类型的符号,或者(例如)只想删除字符串末尾的分号,则应使用正则表达式