【问题标题】:How can I print double quotes ("") in java where parameters are used to print the value? [duplicate]如何在使用参数打印值的java中打印双引号(“”)? [复制]
【发布时间】:2016-11-09 14:44:53
【问题描述】:

下面是我的代码:

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

如何在双引号中打印 something?在这种情况下,我需要解决方案,我们在 System.out.println(); 中使用变量。我需要双引号中那个变量的值。

【问题讨论】:

  • 使用反斜杠转义双引号

标签: java


【解决方案1】:

使用\" 对其进行转义,以便编译器知道不要将您的" 视为划分String 开头或结尾的东西。

【讨论】:

    【解决方案2】:

    这样:

    System.out.println("No of occurrences of '"+ch+"' in \""+str+"\" is "+noOfOcc);
    

    反斜杠作为转义序列打印双引号。
    编译器不解释斜杠符号后的字符。

    【讨论】:

    • System.out.println("'"+ch+"' 在 \""+str+"\""+" 中的出现次数是 "+noOfOcc);谢谢。这对我有用。
    猜你喜欢
    • 2022-01-13
    • 2020-04-11
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多