如何在Java中的字符串中转义双引号

在本文中,我们将看到如何在Java中的String中转义双引号。
There are scenarios where you need to escape double quotes
already present in the 串. This generally happens while dealing with JSON file format 要么 reading file data.
在Java中转义双引号
Double quotes characters can be escaped with backslash(\
) in 爪哇. You can find complete list of 串 escapes over 这个java文档.
让’借助示例了解这一点。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
包 组织.Arpit.爪哇2blog; 上市 类 EscapeDoubleQuotesStringMain { 上市 静态的 虚空 主要(串[] args) { 串 blogName = "爪哇2博客 is 爪哇 blog"; 系统.出.打印(“博客名称:”+blogName); // 让's put 爪哇2博客 in double quotes 串 blogNameWithDoubleQuotes = "\"爪哇2博客\"是Java博客"; 系统.出.打印("BlogName用双引号引起来:"+blogNameWithDoubleQuotes); } } |
输出:
BlogName用双引号引起来:“Java2blog” is 爪哇 blog
💡 你知道吗?
如果将String粘贴在String文字周围的双引号之间,则某些intelij等IDE会自动转义String。
在Java中为String添加双引号
如果要添加双引号(")转换为String,则可以使用String’s 更换() 替换双引号的方法("),双引号前加反斜杠(\")。
这是一个例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
包 组织.Arpit.爪哇2blog; 上市 类 AddDoubleQuotesToStringMain { 上市 静态的 虚空 主要(串[] args) { 串 blogName = "爪哇2博客 is 爪哇 blog"; 系统.出.打印(“博客名称:”+blogName); // 让's put 爪哇2博客 in double quotes 串 blogNameWithDoubleQuotes = blogName.更换("爪哇2博客","\"爪哇2博客\""); 系统.出.打印("BlogName用双引号引起来:"+blogNameWithDoubleQuotes); } } |
输出:
BlogName用双引号引起来:“Java2blog” is 爪哇 blog
在Java中用双引号打印字符串
如果要在java中使用双引号打印String 系统输出文件 ,那么您可以使用 \" 要么 ‘"’ 与字符串。
这是一个例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
包 组织.Arpit.爪哇2blog; 上市 类 PrintDoubleQuotesInStringMain { 上市 静态的 虚空 主要(串[] args) { 串 blogName = "爪哇2博客"; //使用 系统.出.打印(”+blogName+”); //使用'“' 系统.出.打印(“”+blogName+“”); //使用'\ u0022' 系统.出.打印('\ u0022'+blogName+'\ u0022'); } } |
输出:
“Java2blog”
“Java2blog”
那’关于如何在Java中的String中转义双引号的所有内容。