Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String reverse() method



reverse() method creates a new String which is the reverse of this String.

Syntax

String reverse()

Parameters

NA

Return Value

The new value of the string.

Example - Use of reverse() method on String objects

Following is an example of the usage of reverse() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      String a = "Hello World"; 
      println(a.reverse());         
   } 
}

Output

When we run the above program, we will get the following result −

dlroW olleH

Example - Use of reverse() method on String literal

Following is an example of the usage of reverse() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      println("Hello World".reverse());         
   } 
}

Output

When we run the above program, we will get the following result −

dlroW olleH

Example - Use of reverse() method on Palindrome strings

Following is an example of the usage of reverse() method.

Example.groovy

class Example { 
   static void main(String[] args) { 	      
      println("madam".reverse());
	  println("level".reverse());
   } 
}

Output

When we run the above program, we will get the following result −

madam
level
groovy_strings.htm
Advertisements