Stringtemplate. Net learning notes (3): expression element syntax (I)

Source: Internet
Author: User

1. Simple attributes

For simple attributes (different from the set type), The tostring () method is called directly.

Stringtemplate query = new stringtemplate ("$ title $ ");
Query. setattribute ("title", "stringtemplate learning ");
Console. writeline (query. tostring (); output: stringtemplate. Let's look at another example:

Stringtemplate query = new stringtemplate ("$ title $ ");
Query. setattribute ("title", typeof (object ));
Console. writeline (query. tostring (); output: system. Object

2. Index of the Set

String [] strarray = new string [] {"A", "B", "C", "D", "E "};
Stringtemplate query = new stringtemplate ("$ array: {/n current index starts from 1: $ I $, starts from 0: $ I0 $} $ ");
Query. setattribute ("array", strarray );
Console. writeline (query. tostring (); output:

The current index is calculated from 1: 1, and from 0: 0
The current index is calculated from 1: 2, and from 0: 1
The current index is calculated from 1: 3, and from 0: 2
The current index is calculated from 1: 4, and from 0: 3
The current index is calculated from 1: 5, and 0: 4 sets are not limited to arrays. They can be types that implement interfaces such as ilist, icollection, and idictionary.

3. Attribute reference

If attribute is a set or custom type, you can access its attributes through attribute. Property.

Class user {
Public string name {Get; set ;}
Public int age {Get; set ;}
}
Public static void main (string [] ARGs)
{
User u = new user {name = "", age = 1 };
Stringtemplate query = new stringtemplate ("Name: $ user. Name $, age: $ user. Age $ ");
Query. setattribute ("user", U );
Console. writeline (query. tostring ());
} Output: Name: month, age: 1 collection type, which can be a dictionary , Hashtable, and other objects with key/value, such:

Dictionary U = new dictionary (1 );
U. Add ("name", "Monthly ");
U. Add ("Age", "1 ");
Stringtemplate query = new stringtemplate ("Name: $ user. Name $, age: $ user. Age $ ");
Query. setattribute ("user", U );
Console. writeline (query. tostring (); there is another case where the custom set of the attribute st should be set. No detailed study has been conducted:

Stringtemplate ST = new stringtemplate ("$ User: {Name: $ it. Name $, age: $ it. Age $} $ ");
St. setattribute ("user. {name, age}", "", 1 );
Console. writeline (St. tostring ());

When there are reserved words:

Dictionary Items = new dictionary ();
Items. Add ("first", "first ");
Items. Add ("last", "last ");
Items. Add ("1", "Number 1 ");
Stringtemplate ST = new stringtemplate ("$ items. (/"first/") $, $ items. (/"last/") $, $ items. (/"1/") $ ");
St. setattribute ("items", items );
Console. writeline (St. tostring (); output: first, finally, number 1 where first and last are reserved words, number 1 cannot be referenced directly as the property name, which is processed as a reserved word

The following is an example of an error. when the key is of the int type, the number cannot be processed as a reserved word:

Dictionary Dict = new dictionary ();
Dict. Add (1, 11 );
Dict. Add (2, 22 );
Dict. Add (3, 33 );
Stringtemplate ST1 = new stringtemplate ("$ U. (/" 1/") $ ");
St1.setattribute ("u", dict );
Console. writeline (st1.tostring (); the output in this example is empty. You can only obtain the value using the set method.

4. Multiple Attributes/Sets

String [] strarray = new string [] {"A", "B", "C", "D", "E "};
Stringtemplate query = new stringtemplate ("$ array $ ");
Query. setattribute ("array", strarray );
Console. writeline (query. tostring (); output: ABCDE, which is equivalent:

String [] strarray = new string [] {"A", "B", "C "};
Stringtemplate query = new stringtemplate ("$ array $ ");
Query. setattribute ("array", strarray );
Query. setattribute ("array", "D ");
Query. setattribute ("array", "E ");
Console. writeline (query. tostring (); The array is eventually merged into a set.

Type [] typearray = new type [] {typeof (object), typeof (string), typeof (INT), typeof (ilist )};
Stringtemplate query = new stringtemplate ("$ array $ ");
Query. setattribute ("array", typearray );
Console. writeline (query. tostring (); output: system. objectsystem. stringsystem. int32system. collections. the preceding example shows that St calls the tostring () method of each element for the set type and then connects them.

By default, there is no separator. Next, let's take a look at the use of the separator. Double quotation marks must be added on both sides of the separator:

String [] strarray = new string [] {"A", "B", "C", "D", "E "};
Stringtemplate query = new stringtemplate ("$ array; separator =/" --/"$ ");
Query. setattribute ("array", strarray );
Console. writeline (query. tostring (); Result: A -- B -- c -- d -- e when the element in the set is null, this element will not be output, you must specify the null condition:

String [] strarray = new string [] {"A", null, "C", null, "E "};
Stringtemplate query = new stringtemplate ("$ array; null =/" K/", separator =/" --/"$ ");
Query. setattribute ("array", strarray );
Console. writeline (query. tostring (); Result: A -- k -- c -- k -- E (A -- c -- e if null is not specified)

5. Set Merging

For multiple attribute parameters, use square brackets to merge them into a single set:

String [] array1 = new string [] {"A", "B", "C "};
String [] array2 = new string [] {"D", "E "};
Stringtemplate query = new stringtemplate ("$ [list1, list2]: {$ it $}; separator =/",/"$ ");
Query. setattribute ("list1", array1 );
Query. setattribute ("list2", array2 );
Console. writeline (query. tostring (); output result: a-B-c-d-e, we can see that array1 and array2 are merged into one set. If the template definition is changed to $ [list1, list2]: {X, Y | $ x $, $ y $}; separator = "," $ indicates an error.

Let's look at another example. Different element lengths:

Stringtemplate ST1 = new stringtemplate ("$ [a, B, c]: {$ it $}; separator =/",/"$ ");
St1.setattribute ("A", "AA ");
St1.setattribute ("B", new string [] {"B", "C", "D "});
St1.setattribute ("C", typeof (object ));
Console. writeline (st1.tostring (); output result: AA, B, c, d, system. Object. Let's look at another example. merge different types:

Stringtemplate ST1 = new stringtemplate ("$ [a, B, c]: {$ if (it. name) $ it. name $ else $ it $ endif $}; separator =/",/" $ ");
St1.setattribute ("A", new user {name = "AA "});
St1.setattribute ("B", new user [] {New User {name = "B"}, new user {name = "C "}, new User {name = "D "}});
St1.setattribute ("C", typeof (object ));
Console. writeline (st1.tostring (); output result: AA, B, c, d, object can be seen that different types can be merged into the same set

In addition, although there is only one pair of square brackets [] different from $ list1, list2: {X, Y |...} $, it is definitely not the same.

It seems that there is a lot of content, but it should be divided into two parts, saving the trouble.

Reference: http://www.antlr.org/wiki/display/ST/Expressions

Yufeng technology tutorial network http://www.fengfly.com
Http://www.fengfly.com/plus/view-175119-1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.