Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Groovy string


May 14, 2021 Groovy


Table of contents


Reverse constructs a string text in Groovy by including string text in quotation marks.

Groovy offers a variety of ways to represent String literally. S trings in Groovy can be enclosed in single quotes ('), double quotes (') or three quotation marks ('"). In addition, groovy strings enclosed in triple quotes can span multiple lines.

Here's an example of string use 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 program above, we will get the following results -

Hello Single 
Hello Double 
'Hello TripleMultiple lines'

The string index

The string in Groovy is an ordered sequence of characters. A single character in a string can be accessed through its position. This is given by the index location.

The string index starts at zero and ends with less than one of the string lengths. Groovy also allows negative indexes to count from the end of the string.

Here's an example of the use of string indexes 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 program above, we will get the following results -

o 
d 
el 
oll 

The basic string operation

First let's learn the basic string operations in groovy. They are given below.

Serial number String operations and descriptions
1 Concatenation of two strings

The series of strings can be done with a simple 'plus' operator.

2 String Repetition

The repetition of strings can be done with a simple ''' operator.

3 String Length

The length of the string determined by the string's length() method.

String method

Here is a list of methods supported by the String class.

Serial number Method and description
1 center()

Returns a new string of numberOfChars, which consists of recipients filled with space characters on the left and right.

2 compareToIgnoreCase()

Compare two strings in alphabetical order, ignoring case differences.

3 concat()

Connect the specified String to the end of this String.

4 eachMatch()

Substrings for a given String that handle each regular expression group (see next section).

5 endsWith()

Test whether this string ends with the specified suffix.

6 equalsIgnoreCase()

Compare this string to another string, ignoring case considerations.

7 getAt()

It returns a string value at the index location

8 indexOf()

Returns the index in this string that specifies the first time a substring appears.

9 matches()

It outputs whether the string matches a given regular expression.

10 minus()

Delete the value portion of the string.

11 next()

This method is called by the String class by the operator. It adds the last character in a given string.

12 padLeft()

Fills the string and adds spaces to the left.

13 padRight()

Fills the string and adds spaces to the right.

14 plus()

Append the string

15 previous()

This method is called by CharSequence's - operator.

16 replaceAll()

Replace all appearances of the captured group with the result of closing the text.

17 reverse()

Create a new string that is the opposite of String.

18 split()

Split this String into matches for a given regular expression.

19 subString()

Returns a new String, which is a substring of this String.

20 toUpperCase()

Convert all characters in this string to capital.

21 toLowerCase()

Convert all characters in this string to small case.