Introduction

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

StringTemplate will iterate across any object that it can reasonably interpret as a collection of elements such as arrays, lists, dictionaries and, in statically typed ports, objects satisfying iterable or enumeration interfaces.

General use of StringTemplate for formatting

The ST.format method is great for general use in general code.

Deck of Cards
idexample3
Card
labelJava
Code Block

int[] num =
    new int[] {3,9,20,2,1,4,6,32,5,6,77,888,2,1,6,32,5,6,77,
        4,9,20,2,1,4,63,9,20,2,1,4,6,32,5,6,77,6,32,5,6,77,
        3,9,20,2,1,4,6,32,5,6,77,888,1,6,32,5};
String t =
    ST.format(30, "int <%1>[] = { <%2; wrap, anchor, separator=\", \"> };", "a", num);
System.out.println(t);
Card
labelC#
Code Block
Card
labelPython
Code Block

Yields:

Code Block

int a[] = { 3, 9, 20, 2, 1, 4,
            6, 32, 5, 6, 77, 888,
            2, 1, 6, 32, 5, 6,
            77, 4, 9, 20, 2, 1,
            4, 63, 9, 20, 2, 1,
            4, 6, 32, 5, 6, 77,
            6, 32, 5, 6, 77, 3,
            9, 20, 2, 1, 4, 6,
            32, 5, 6, 77, 888,
            1, 6, 32, 5 };