如何在java中打印双引号(“双引号”),其中参数用于打印值?
下面是我的代码:如何在java中打印双引号(“双引号”),其中参数用于打印值?,java,Java,下面是我的代码: String str = "something"; char ch = 'e'; int noOfOcc = 1; System.out.println("No of occurrences of '"+ch+"' in "+str+" is "+noOfOcc); 预期产出应如下所示: No of occurrences of 'e' in "something" is 1 我怎么能用双引号把它打印出来?我需要在System.out.println()中使用变量的这种情况
String str = "something";
char ch = 'e';
int noOfOcc = 1;
System.out.println("No of occurrences of '"+ch+"' in "+str+" is "+noOfOcc);
预期产出应如下所示:
No of occurrences of 'e' in "something" is 1
我怎么能用双引号把它打印出来?我需要在System.out.println()中使用变量的这种情况的解决方案;。我需要双引号中该变量的值。使用\“
对其进行转义,以便编译器知道不要将您的“
视为用以下方式来划分字符串的开始或结束:
System.out.println("No of occurrences of '"+ch+"' in \""+str+"\" is "+noOfOcc);
反斜杠作为转义序列打印双引号。
编译器不会解释斜杠符号后的字符。使用反斜杠转义双引号System.out.println(“在\''+str+'\'+'中'+ch+'的出现次数为'+noOfOcc”);谢谢。这对我有用。