String in Groovy

A string is a sequence of characters. Generally, a string is a data type and implemented as an array of bytes which stores sequence of characters. In Groovy, String class is available in java.lang.String objects as well as in groovy.lang.GString which is called interpolated strings in some programming language.

Note: In groovy, all the strings can be concatenated by using the + operator

Single-quoted string

In groovy, Single-quoted strings is a sequence of characters which is enclosed under single quotes.

Syntax:

Example 1:

Output:

String in Groovy

Double-quoted string

In groovy, Double-quoted string is a sequence of characters which is enclosed under double quotes.

Syntax:

Example 2:

Output:

String in Groovy

In Groovy, we can also use ${Variable _name} and $Variable_name instead of using the '+' operator. Using $Variable_name is known as interpolation. It can be used only when the string is defined inside the double quotes.

Example 3:

Output:

String in Groovy

Triple-single-quoted string

In groovy, Triple-single-quoted string is a sequence of characters which is enclosed under triple single quotes.

Syntax:

Example 4:

Output:

String in Groovy

In Groovy, Triple-single-quoted strings are mostly used for multiple lines in a single variable.

Example 5:

Output:

String in Groovy

Triple-double-quoted string

In groovy, Triple-double-quoted string is a sequence of characters which is enclosed under triple double quotes.

Syntax:

Example 6:

Output:

String in Groovy

In Groovy, Triple-double-quoted strings are also used for multiple lines in a single variable.

Example 7:

Output:

String in Groovy

In Triple-double-quoted strings, we can also use ${Variable _name} and $Variable_name instead of using the '+' operator. Using $Variable_name is known as interpolation.

Example 8:

Output:

String in Groovy

Slashy string

In groovy, slashy string is a sequence of characters which is enclosed in / forward slash. Slashy strings are useful for defining regular expressions and patterns where there is no need to escape backslashes.

Syntax:

Example 9:

Output:

String in Groovy

In Groovy, slashy strings are also used for multiple lines in a single variable.

Example 10:

Output:

String in Groovy

In slashy strings we can also use ${Variable _name} and $Variable_name instead of using the '+' operator. Using $Variable_name is known as interpolation.

Example 11:

Output:

String in Groovy

Dollar slashy string

In groovy, dollar slashy string is a sequence of characters which is enclosed in the $/ dollar and a forward slash. Dollar slashy strings are useful for defining regular expressions and patterns where there is no need to escape backslashes.

Syntax:

Example 12:

Output:

String in Groovy

In Groovy, dollar slashy strings are also used for multiple lines in a single variable.

Example 13:

Output:

String in Groovy

In dollar slashy strings, we can also use ${Variable _name} and $Variable_name instead of using the '+' operator. Using $Variable_name is known as interpolation.

Example 11:

Output:

String in Groovy
Next Topic#