2

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(e.g), 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:

e.g:

    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
BigBang
  • 149
  • 12
  • You could use an `ArrayList` instead of a plain array. – André Stannek Jan 14 '15 at 13:12
  • 1
    Probably your actual format is more complicated. But in this particular case you might want just to join strings: http://stackoverflow.com/questions/187676/java-equivalents-of-c-sharp-string-format-and-string-join – default locale Jan 14 '15 at 13:14
  • I think this will not make any difference, as still in ArrayList my values may be less than the position parameters provided in the pattern....@Andre – BigBang Jan 14 '15 at 13:17
  • yes absolute my format is more complicated, as this pattern will be define globally and every one below should follow to format their string in this format – BigBang Jan 14 '15 at 13:25
  • 1
    Should all the non-placeholder parts of the template remain? Or should the complete template be truncated from the last filled-in placeholder? In other words, do you really want "abc, cde, gef" or do you want "abc, cde, gef,,"? – Adriaan Koster Jan 14 '15 at 13:26
  • All non place holder should be removed from the final output, Yes I want the strings as -- "abc, cde, gef" – BigBang Jan 14 '15 at 13:29
  • @BigBang It's unclear which placeholders should be removed. Suppose you have a pattern like this: `{0},{1}&{2}-0_o-{3}-here-comes-fourth-{4}_message_to_user_{5}-again-{3},{2},{1}`. What should happen if there're only 3 arguments? – default locale Jan 14 '15 at 13:38
  • then third, fourth and fifth(3,4, 5) should be removed and 0,1,2 should be populated – BigBang Jan 14 '15 at 13:41
  • @BigBang it's clear, but what about the placeholders? – default locale Jan 14 '15 at 13:48
  • they should not be there – BigBang Jan 14 '15 at 13:54
  • Actually the format is of csv file, so what i want is the header and the children should follow the same structure, and in the future if I need to change the sequence, then I only need to change the pattern. – BigBang Jan 14 '15 at 13:56

1 Answers1

0
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));
Vitaly
  • 2,552
  • 2
  • 18
  • 21