Groovy - Strings


Advertisements


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 Concatenation of two strings

The concatenation of strings can be done by the simple ‘+’ operator.

2 String Repetition

The repetition of strings can be done by the simple ‘*’ operator.

3 String Length

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 center()

Returns a new String of length numberOfChars consisting of the recipient padded on the left and right with space characters.

2 compareToIgnoreCase()

Compares two strings lexicographically, ignoring case differences.

3 concat()

Concatenates the specified String to the end of this String.

4 eachMatch()

Processes each regex group (see next section) matched substring of the given String.

5 endsWith()

Tests whether this string ends with the specified suffix.

6 equalsIgnoreCase()

Compares this String to another String, ignoring case considerations.

7 getAt()

It returns string value at the index position

8 indexOf()

Returns the index within this String of the first occurrence of the specified substring.

9 matches()

It outputs whether a String matches the given regular expression.

10 minus()

Removes the value part of the String.

11 next()

This method is called by the ++ operator for the class String. It increments the last character in the given String.

12 padLeft()

Pad the String with the spaces appended to the left.

13 padRight()

Pad the String with the spaces appended to the right.

14 plus()

Appends a String

15 previous()

This method is called by the -- operator for the CharSequence.

16 replaceAll()

Replaces all occurrences of a captured group by the result of a closure on that text.

17 reverse()

Creates a new String which is the reverse of this String.

18 split()

Splits this String around matches of the given regular expression.

19 subString()

Returns a new String that is a substring of this String.

20 toUpperCase()

Converts all of the characters in this String to upper case.

21 toLowerCase()

Converts all of the characters in this String to lower case.



Advertisements
E-Books Store