The gstring of groovy exploration

Source: Internet
Author: User

The groovy language has a lot of seemingly obscure gadgets, but after use, we often marvel at its enormous energy, and gstring is one of them.

The Java string object is our most common object, but it is also the most criticized object. In a word, string is very inconvenient to use. Gstring is not only easy to use, but also the basis of the dynamic nature of groovy language.

The so-called gstring refers to strings with placeholder expressions that are enclosed in double quotes, such as def str = "${name} ' dog is ${dog.name}", where the dollar sign is a placeholder expression, and Str is a Gstring object.

Here, try to list some of the common methods.

1. String Overlay

The Java string superposition of strings is accomplished by adding the string object in the form of the following:

def result = name+” is ”+age+” years old”;

This form of string addition makes us very inconvenient when coding, and gstring is different, it can make us very convenient to encode, the form is as follows:

def result = “$name is $age years old”;

2. Convenient operation of strings and functions

In the coding process, we often work with the expression results and strings together, gstring can make us more convenient and lightweight in this calculation. Take a look at the following example:

def result = “key: person.name value: ${map[‘person.name’]}”
def result = “${book?.author?.name}”

Wait a minute

The dynamic nature of 3.Gstring

Groovy language programming is very dynamic and allows us to write very elegant low-level abstract code. And gstring is the basis of this dynamic programming.

In the area of reflection, using gstring makes it easy to access the properties and methods of a class. As follows:

Let's say we have a person class

class Person
{
String name
}

Here we can manipulate the properties of the person's object by gstring the object.

String propName = ‘name’;
Person p = new Person()
p.”${ propName }” = ‘tom’

println p.”${propName}”

Also, by Gstring objects, we can easily manipulate the object's methods as follows:

Let's assume that the person class above has a ToString () method.

public String toString()
{
return name
}

So, we can do the following for this method.

String functionName = “toString”
Person p = new Person()
p.name = “mike”

println p.”${functionName}”()

In the case of regular expressions, we can also use the Gstring object to perform run-time substitution of regular expressions, which is also an interesting work done by Gstring, as follows:

def reference = “pattern”

println reference == /${reference}/

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.