Java 是否有方法获取MessageFormat对象模式的最大索引参数?
是否有方法获取MessageFormat对象模式的最大索引参数? 例如:Java 是否有方法获取MessageFormat对象模式的最大索引参数?,java,messageformat,Java,Messageformat,是否有方法获取MessageFormat对象模式的最大索引参数? 例如: “亲爱的{0},一些文本{0}text”->1 “亲爱的{0},一些文本{1}text”->2 “{0,choice,0#0 User | 1#User | 1如果使用过一次,可以使用: MessageFormat format = new MessageFormat("Hello {0}, {1}"); // here somewhere you use it System.out.println(String.fo
“亲爱的{0},一些文本{0}text”
->1“亲爱的{0},一些文本{1}text”
->2“{0,choice,0#0 User | 1#User | 1如果使用过一次,可以使用:
MessageFormat format = new MessageFormat("Hello {0}, {1}");
// here somewhere you use it
System.out.println(String.format("Get max index -- %d", new HashSet(Arrays.asList(format.getFormats())).size()));
这张照片是:
获取最大索引--2
。基本上,在将字符串传递给MessageFormat之前,在字符串上使用正则表达式。你能帮我用同样的表达式编写正则表达式吗?如果你深入了解你的示例,你可能会注意到它没有涵盖示例3。提前谢谢你。不,我没有时间。很好UCKAR你确定第三个是正确的吗?不应该是“{0,choice,0#0 User | 1#User | 1如果你想知道消息格式在使用之前有多少占位符,我想你别无选择:regexp;否则如果消息格式在别处使用过一次,你可以使用新的HashSet(Arrays.asList(messageformat.getFormats()).size()
谢谢,你说得对,但是对于相同的值format=newmessageformat(“Hello{0},{0}”);它还打印2