Groovy – Strings

  • Post author:
  • Post category:Groovy
  • Post comments:0 Comments
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'

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
1Concatenation of two strings
The concatenation of strings can be done by the simple β€˜+’ operator.
2String Repetition
The repetition of strings can be done by the simple β€˜*’ operator.
3String 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
1center()
Returns a new String of length numberOfChars consisting of the recipient padded on the left and right with space characters.
2compareToIgnoreCase()
Compares two strings lexicographically, ignoring case differences.
3concat()
Concatenates the specified String to the end of this String.
4eachMatch()
Processes each regex group (see next section) matched substring of the given String.
5endsWith()
Tests whether this string ends with the specified suffix.
6equalsIgnoreCase()
Compares this String to another String, ignoring case considerations.
7getAt()
It returns string value at the index position
8indexOf()
Returns the index within this String of the first occurrence of the specified substring.
9matches()
It outputs whether a String matches the given regular expression.
10minus()
Removes the value part of the String.
11next()
This method is called by the ++ operator for the class String. It increments the last character in the given String.
12padLeft()
Pad the String with the spaces appended to the left.
13padRight()
Pad the String with the spaces appended to the right.
14plus()
Appends a String
15previous()
This method is called by the — operator for the CharSequence.
16replaceAll()
Replaces all occurrences of a captured group by the result of a closure on that text.
17reverse()
Creates a new String which is the reverse of this String.
18split()
Splits this String around matches of the given regular expression.
19subString()
Returns a new String that is a substring of this String.
20toUpperCase()
Converts all of the characters in this String to upper case.
21toLowerCase()
Converts all of the characters in this String to lower case.

Next Topic:-Click Here

Leave a Reply