Java MessageFormat-如何在单引号之间插入值? [英] Java MessageFormat - How can I insert values between single quotes?

查看:376
本文介绍了Java MessageFormat-如何在单引号之间插入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用java.text.MessageFormat对象时遇到问题.

I'm having a problem using the java.text.MessageFormat object.

我正在尝试创建SQL插入语句.问题是,当我做这样的事情时:

I'm trying to create SQL insert statements. The problem is, when I do something like this:

MessageFormat messageFormat = "insert into {0} values ( '{1}', '{2}', '{3}', {4} )";
Object[] args = { str0, str1, str2, str3, str4 };
String result = messageFormat.format(args);

我得到的是result的值:

"insert into <str0> values ( {1}, {2}, {3}, <str4> )"

如您所见,问题在于单引号引起的任何目标位置都不会被参数替换.我试过使用双引号这样的:''{1}''和转义的字符是这样的:\'{1}\',但是它仍然给出相同的结果.

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}'''.结果是:"insert into <str0> values ( '{1}', '{2}', '{3}', <str4> )".它会保留原始引号,但仍不会插入值.

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.

如何解决此问题?作为记录,我使用的是JDK 6u7.

How can I resolve this issue? For the record, I am using JDK 6u7.

推荐答案

我刚刚尝试了双引号,但对我来说效果很好:

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);

结果是:

insert into 000 values ( '111', '222', '333', 444 )

这是您需要的吗?

这篇关于Java MessageFormat-如何在单引号之间插入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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