英文:
Concatenate String with double quotes in Java
问题
现在我有一个方法,它将返回XPath值,即:
public String xpathValue(String countryName) {
return "//*[@label=\"" + countryName + "\"]";
}
如果我使用Nigeria调用它,它将返回以下值:
//*[@label=Nigeria]
但我想要值用双引号括起来,就像这样:
//*[@label="Nigeria"]
我该如何实现这个目标?
英文:
Now I have method, which will return me xpath value i.e.
public String xpathValue (String countryName){
return "//*[label="+countryName+"]"
}
And if I call it with Nigeria it will return me value like this:
//*[@label=Nigeria]
But I want value surrounded with double quotes, like this:
//*[@label="Nigeria"]
How can I achieve this?
答案1
得分: 1
A valid xpath is:
//*[@label='Nigeria']
and to generate the above, you can use the following line of code:
return "//*[@label='" + countryName + "']";
and to generate:
//*[@label="Nigeria"]
You can use the following line of code:
return "//*[@label=\"" + countryName + "\"]";
// ^the above backslash indicates that the following character i.e. \" is printable
POC
Code:
public class XPathStringDemo {
public static void main(String[] args) {
XPathStringDemo obj = new XPathStringDemo();
System.out.println(obj.xpathValue("Nigeria"));
}
public String xpathValue (String countryName){
return "//*[@label=\"" + countryName + "\"]";
}
}
Output:
//*[@label="Nigeria"]
英文:
A valid xpath is:
//*[@label='Nigeria']
and to generate the above, you can use the following line of code:
return "//*[@label='"+countryName+"']";
and to generate:
//*[@label="Nigeria"]
You can use the following line of code:
return "//*[@label=\""+countryName+"\"]";
// ^the above backslash indicates that the following character i.e. " is printable
POC
Code:
public class XPathStringDemo {
public static void main(String[] args) {
XPathStringDemo obj = new XPathStringDemo();
System.out.println(obj.xpathValue("Nigeria"));
}
public String xpathValue (String countryName){
return "//*[@label=\""+countryName+"\"]";
}
}
Output:
//*[@label="Nigeria"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论