Groovy String reverse() examples

Groovy is fun to use because it gives utility methods out of the box without us requiring any special external library, and without needing to write out own code. Mostly are simple and very intuitive. We explore one method in this example. In this post, we will check how to use Groovy String reverse method.


Syntax

Below is the syntax from the Groovy documentation here
public static String  reverse(String  self)
Which we could simplify like this:
public static String  reverse()
Because self is not a real parameter when we use this method in the String class.


Examples

The Groovy String class reverse method just reverses the characters of a String and returns it to the caller. See below simple example:

println "rock".reverse()

If we run this simple script, we will get below output
kcor
It is that simple as the documentation says that this method creates a new String which is the reverse (backwards) of this string. So in fact, this will return a new String with characters reversed. And also according to doc, this method has been implemented since Groovy version 1.8.2