I have a pattern like :
String pattern = "{0},{1},{2},{3},{4},{5},{6},{7}";
I have a Array of values to get Populated in this pattern:
Object[] values = new String[8];
This Array of values is dynamic , sometimes it gets populated with full 8 allotted slots and some times it is less than that, but never more than allotted size.
Now when it is less than 8 slots(eg), and when i format it using
MessageFormat messageFormat = new MessageFormat(pattern);
header = new StringBuffer();
messageFormat.format(values, header, null);
theirs remains those position whose substitutes are not found:
eg:
String pattern = "{0},{1},{2},{3},{4}";
Object[] values = {"abc", "cde", "gef"};
MessageFormat messageFormat = new MessageFormat(pattern);
StringBuffer header = new StringBuffer();
System.out.println(messageFormat.format(values, header, null));
output
abc,cde,gef,{3},{4}
But what I want is those position should also be moved: As final output should be like this:
abc, cde, gef
String pattern = "{0},{1},{2},{3},{4}";
String[] patternArray = pattern.split("}");
Object[] values = {"abc", "cde", "gef"};
StringBuilder patternBuilder = new StringBuilder();
for(int i = 0; i < values.length; ++i){
patternBuilder.append(patternArray[i]).append("}");
}
pattern = patternBuilder.toString();
MessageFormat messageFormat = new MessageFormat(pattern);
StringBuffer header = new StringBuffer();
System.out.println(messageFormat.format(values, header, null));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.