我使用 HtmlUnit 从 xpath 中提取了数据并得到了这个:
String[] data = new String[10]; // number of columns
data[0] = page.querySelector(".sprop-product-heading").getTextContent().trim().toString();
数据[d]...等等。
但是当我在 lastIndexOf 中使用 data[0] 作为参数时,它返回 -1。
int posProd = someList.toString().lastIndexOf(data[0]); //returns -1
但是当我使用“引用中的搜索词”作为 lastIndexOf 的参数时,它会返回确切的位置。
int posProd = someList.toString().lastIndexOf("Searched Words in quotations"); //returns the index position
我已经尝试将数据 [0] 保存在另一个变量上,但它仍然不起作用,它返回 -1。
String prodname = String.valueOf(data[0]);
int posProd = someList.toString().lastIndexOf(prodname); //returns -1
如何在不使用“”的情况下使用 data[0] 作为 lastIndexOf 中的参数?
编辑:
例子:
数据[0] = "Lorem Ipsum"
String someList = "一个由来已久的事实是,读者在查看其布局时会被页面的可读内容分散注意力。使用 Lorem Ipsum 的重点是它或多或少具有正态分布的字母,而不是使用‘这里的内容,这里的内容’,让它看起来像可读的英语。”
int posProd = someList.toString().lastIndexOf(data[0]); //returns -1
但是当我测试它时:
int posProd = someList.toString().lastIndexOf("Lorem Ipsum"); //returns the position
编辑2:
List<HtmlDivision> productDesc = page.getByXPath("//div[@class='col-md-6 md-margin-bottom-10']//following-sibling::div");
String productDescList = "";
for(HtmlDivision prodName:productDesc){
productDescList = productDescList.trim()+prodName.asText().trim();
}
System.out.println("productDescList: " +productDescList);
String prodname = String.valueOf(data[0]);
System.out.println("prodname: " +prodname.trim());
int posProd = productDescList.toString().lastIndexOf(prodname);
String cleanDesc = productDescList.substring(posProd, productDescList.length()-5);
System.out.println("cleanDesc: " +cleanDesc);