在Java中转义双引号 [英] Escape double quotes in Java

查看:1160
本文介绍了在Java中转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:






  private static final String CREATE_TABLE_EXHIBITORS =CREATE TABLEusers (_idtext PRIMARY KEY,nametext,bodytext,imagetext,stand_idtext,beginlong,endlong,changedatelong Facebook文本,myspace文本,twitter文本,festivallink文本,最爱整数);; 

假设我希望这个查询是Java中的有效字符串。我必须转换它才能摆脱前面带有尾部斜杠的所有双引号?

  =CREATE TABLE \users\ 

还是有一个更快的方法来使整体一次查询一个有效的字符串?我以为你可以在整个字符串周围使用单引号来做到这一点,但是也不行。

解决方案>

使用反斜杠转义双引号是在Java中执行此操作的唯一方法。



某些IDE如将这样的字符串粘贴到一个字符串文字中(即在java字符串之间的双引号之间)时,会自动执行此转义。



另一个选项是将String放入某种文本文件,然后您可以在运行时读取$ /

Possible Duplicate:
In Java, is there a way to write a string literal without having to escape quotes?

private static final String CREATE_TABLE_EXHIBITORS = "CREATE TABLE "users" ("_id" text PRIMARY KEY ,"name" text,"body" text,"image" text,"stand_id" text,"begin" long,"end" long,"changedate" long,"website" text,"facebook" text,"myspace" text,"twitter" text,"festivallink" text,"favorite" integer);";

Let's say I want this query to be a valid string in Java. Do I have to convert it to escape all double quotes with a trailing slash in front?

e.g.  = "CREATE TABLE \"users"\

Or is there a faster way to make this whole query a valid string at once? I thought you could use single quotes around the whole string to do that but that doesn't work either.

解决方案

Escaping the double quotes with backslashes is the only way to do this in Java.

Some IDEs around such as IntelliJ IDEA do this escaping automatically when pasting such a String into a String literal (i.e. between the double quotes surrounding a java String literal)

One other option would be to put the String into some kind of text file that you would then read at runtime

这篇关于在Java中转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆