Groovy - Strings

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'

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 

First let’s learn the basic string operations in groovy. They are given below.
The concatenation of strings can be done by the simple ‘+’ operator.
The repetition of strings can be done by the simple ‘*’ operator.
The length of the string determined by the length() method of the string.
Here is the list of methods supported by String class.
Returns a new String of length numberOfChars consisting of the recipient padded on the left and right with space characters.
Compares two strings lexicographically, ignoring case differences.
Concatenates the specified String to the end of this String.
Processes each regex group (see next section) matched substring of the given String.
Tests whether this string ends with the specified suffix.
Compares this String to another String, ignoring case considerations.
It returns string value at the index position
Returns the index within this String of the first occurrence of the specified substring.
It outputs whether a String matches the given regular expression.
Removes the value part of the String.
This method is called by the ++ operator for the class String. It increments the last character in the given String.
Pad the String with the spaces appended to the left.
Pad the String with the spaces appended to the right.
Appends a String
This method is called by the -- operator for the CharSequence.
Replaces all occurrences of a captured group by the result of a closure on that text.
Creates a new String which is the reverse of this String.
Splits this String around matches of the given regular expression.
Returns a new String that is a substring of this String.
Converts all of the characters in this String to upper case.
Converts all of the characters in this String to lower case.

    52 Lectures 
     8 hours 

 Krishna Sakinala

    49 Lectures 
     2.5 hours 

 Packt Publishing
 Print
 Add Notes
 Bookmark this page