Java MessageFormat - 如何在单引号之间插入值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/189889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java MessageFormat - How can I insert values between single quotes?
提问by TM.
I'm having a problem using the java.text.MessageFormat object.
我在使用 java.text.MessageFormat 对象时遇到问题。
I'm trying to create SQL insert statements. The problem is, when I do something like this:
我正在尝试创建 SQL 插入语句。问题是,当我做这样的事情时:
MessageFormat messageFormat = "insert into {0} values ( '{1}', '{2}', '{3}', {4} )";
Object[] args = { str0, str1, str2, str3, str4 };
String result = messageFormat.format(args);
I get this for the value of result
:
我得到这个的价值result
:
"insert into <str0> values ( {1}, {2}, {3}, <str4> )"
As you can see, the problem is that any of the target locations that are enclosed by single quotes do not get replaced by arguments. I have tried using double single quotes like this: ''{1}''
and escaped characters like this: \'{1}\'
but it still gives the same result.
如您所见,问题在于单引号括起来的任何目标位置都不会被参数替换。我试过使用这样的双单引号:''{1}''
和这样的转义字符:\'{1}\'
但它仍然给出相同的结果。
edit: I forgot to mention that I also tried '''{1}'''
. The result is: "insert into <str0> values ( '{1}', '{2}', '{3}', <str4> )"
. It is keeping the original quotes around but still not inserting the values.
编辑:我忘了提到我也试过'''{1}'''
. 结果是:"insert into <str0> values ( '{1}', '{2}', '{3}', <str4> )"
。它保留了原始引号,但仍未插入值。
How can I resolve this issue? For the record, I am using JDK 6u7.
我该如何解决这个问题?作为记录,我使用的是 JDK 6u7。
采纳答案by serg
I just tried double quotes and it worked fine for me:
我刚试过双引号,对我来说效果很好:
MessageFormat messageFormat = new MessageFormat("insert into {0} values ( ''{1}'', ''{2}'', ''{3}'', {4} )");
Object[] args = {"000", "111", "222","333","444","555"};
String result = messageFormat.format(args);
The result is:
结果是:
insert into 000 values ( '111', '222', '333', 444 )
Is this what you need?
这是你需要的吗?
回答by billjamesdev
First thing that came to mind was to change str1, str2, str3 to have the single quotes around them.
首先想到的是将 str1、str2、str3 更改为在它们周围加上单引号。
Object[] args = { str0, "'" + str1 + "'", "'" + str2 + "'", "'" + str3 + "'", str4 };
Then, of course, remove the single-quotes from your query string.
然后,当然,从查询字符串中删除单引号。
回答by Chris Boran
Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string
"'{0}'"
represents string"{0}"
, not a FormatElement. A single quote itself must be represented by doubled single quotes''
throughout aString
. For example, pattern string"'{''}'"
is interpreted as a sequence of'{
(start of quoting and a left curly brace),''
(a single quote), and}'
(a right curly brace and end of quoting), not'{'
and'}'
(quoted left and right curly braces): representing string"{'}"
, not"{}"
.
在String 中,一对单引号可用于引用除单引号之外的任意字符。例如,模式 string
"'{0}'"
表示 string"{0}"
,而不是FormatElement。单引号本身必须在''
整个String
. 例如,模式字符串"'{''}'"
被解释为'{
(引号的开始和左花括号)、''
(单引号)和}'
(右花括号和引号的结尾),而不是'{'
和'}'
(左花括号和右花括号)的序列: 代表字符串"{'}"
,而不是"{}"
。
回答by Brian Duff
Use triple single quote characters:
使用三重单引号字符:
MessageFormat messageFormat = "insert into {0} values ( '''{1}''', '''{2}''', '''{3}''', '''{4}''' )";
回答by Aidos
Sorry if this is off the side, but it looks like you're trying to replicate the PreparedStatement that is already in JDBC.
抱歉,如果这不合适,但看起来您正在尝试复制 JDBC 中已经存在的 PreparedStatement。
If you are trying to create SQL to run against a database then I suggest that you look at PreparedStatement, it already does what you're trying to do (with a slightly different syntax).
如果您正在尝试创建 SQL 以针对数据库运行,那么我建议您查看 PreparedStatement,它已经完成了您想要做的事情(语法略有不同)。
Sorry if this isn't what you are doing, I just thought I would point it out.
对不起,如果这不是你在做什么,我只是想我会指出。