A String literal is constructed in Groovy by enclosing the string text in quotations.
Groovy offers a variety of ways to denote a String literal. Strings in Groovy can be enclosed in single quotes (’), double quotes (“), or triple quotes (“””). Further, a Groovy String enclosed by triple quotes may span multiple lines.
Following is an example of the usage of strings in Groovy −
class Example { static void main(String[] args) { String a = ''Hello Single String b = "Hello Double"; String c = "''Hello Triple" + "Multiple lines''"; println(a); println(b); println(c); } }
When we run the above program, we will get the following result −
Hello Single Hello Double ''Hello TripleMultiple lines''
String Indexing
Strings in Groovy are an ordered sequences of characters. The individual character in a string can be accessed by its position. This is given by an index position.
String indices start at zero and end at one less than the length of the string. Groovy also permits negative indices to count back from the end of the string.
Following is an example of the usage of string indexing in Groovy −
class Example { static void main(String[] args) { String sample = "Hello world"; println(sample[4]); // Print the 5 character in the string //Print the 1st character in the string starting from the back println(sample[-1]); println(sample[1..2]);//Prints a string starting from Index 1 to 2 println(sample[4..2]);//Prints a string starting from Index 4 back to 2 } }
When we run the above program, we will get the following result −
o d el oll
Basic String Operations
First let’s learn the basic string operations in groovy. They are given below.
S.No. | String Operation & Description |
---|---|
1 |
The concatenation of strings can be done by the simple ‘+’ operator. |
2 |
The repetition of strings can be done by the simple ‘*’ operator. |
3 |
The length of the string determined by the length() method of the string. |
String Methods
Here is the list of methods supported by String class.
S.No. | Methods & Description |
---|---|
1 |
Returns a new String of length numberOfChars consisting of the recipient padded on the left and right with space characters. |
2 |
Compares two strings lexicographically, ignoring case differences. |
3 |
Concatenates the specified String to the end of this String. |
4 |
Processes each regex group (see next section) matched substring of the given String. |
5 |
Tests whether this string ends with the specified suffix. |
6 |
Compares this String to another String, ignoring case considerations. |
7 |
It returns string value at the index position |
8 |
Returns the index within this String of the first occurrence of the specified substring. |
9 |
It outputs whether a String matches the given regular expression. |
10 |
Removes the value part of the String. |
11 |
This method is called by the ++ operator for the class String. It increments the last character in the given String. |
12 |
Pad the String with the spaces appended to the left. |
13 |
Pad the String with the spaces appended to the right. |
14 |
Appends a String |
15 |
This method is called by the — operator for the CharSequence. |
16 |
Replaces all occurrences of a captured group by the result of a closure on that text. |
17 |
Creates a new String which is the reverse of this String. |
18 |
Splits this String around matches of the given regular expression. |
19 |
Returns a new String that is a substring of this String. |
20 |
Converts all of the characters in this String to upper case. |
21 |
Converts all of the characters in this String to lower case. |