Groovy 3 was released a few days ago[1], and it introduced a lot of important new features to the language. Today I want to show you a few useful improvements in the GDK. We will take a closer look into methods like takeRight, takeAfter, takeBetween, and a few others that were added to the java.lang.String class.

In this blog post we use String as a base class, but most (if not all) of presented methods are working with String, CharSequence and GString classes.

String.takeRight(int)

Let’s start with the first one - takeRight. This method allows you to extract n last characters from a given String (or all characters if the number is larger then the string length.)

final String text = "Groovy"
assert text.takeRight(0) == ""
assert text.takeRight(1) == "y"
assert text.takeRight(3) == "ovy"
assert text.takeRight(20) == "Groovy"

String.takeAfter(str)

This method allows you to extract the text that exists after the first occurrence of the str. Keep in mind that it is case-sensitive, so it looks for the exact match.

final String text = "Groovy"
assert text.takeAfter("G") == "roovy"
assert text.takeAfter("g") == ""
assert text.takeAfter("Gro") == "ovy"
assert text.takeAfter("Groovy") == ""

String.takeBefore(str)

It is similar to takeAfter, but here it extracts the text that exists before the first occurrence of str.

final String text = "Groovy"
assert text.takeBefore("G") == ""
assert text.takeBefore("g") == ""
assert text.takeBefore("ovy") == "Gro"
assert text.takeBefore("o") == "Gr"
assert text.takeBefore("Groovy") == ""

String.takeBetween(from,to)

This method allows you to extract the text that exists between the first occurrence of from and to. It can be used with a single parameter, then to becomes from. There is also a third optional parameter - occurrence which defines which occurrence should be taken into account (default: the first occurrence of from and to).

final String text = "Lorem ipsum dolor sit amet"

assert text.takeBetween("i") == "psum dolor s"
assert text.takeBetween("i", "r") == "psum dolo"
assert text.takeBetween("i", "a") == "psum dolor sit "
assert text.takeBetween("l","o") == ""
assert text.takeBetween("m")  == " ipsu"
assert text.takeBetween("m", 1) == ""
assert text.takeBetween("i", "m", 0) == "psu"
assert text.takeBetween("i", "m", 1) == "t a"
assert text.takeBetween("i", "m", 2) == ""

String.dropRight(int)

This is an equivalent of String.drop(int) method, but in this case it produces a new String that drops n characters from the right side.

final String text = "Hello, World!"
assert text.dropRight(4) == "Hello, Wo"
assert text.dropRight(0) == "Hello, World!"
assert text.dropRight(-10) == "Hello, World!"
assert text.dropRight(20) == ""

String.startsWithIgnoreCase(str) and similar

Groovy also adds "ignore case" variants to three popular String methods:

  • String.startsWithIgnoreCase(str)

  • String.endsWithIgnoreCase(str)

  • String.containsIgnoreCase(str)

final String text = "Hello, World!"

assert text.startsWithIgnoreCase("he") == true
assert text.startsWithIgnoreCase("HE") == true
assert text.startsWithIgnoreCase("HEE") == false
assert text.endsWithIgnoreCase("D!") == true
assert text.endsWithIgnoreCase("LD!") == true
assert text.endsWithIgnoreCase("LLD!") == false
assert text.containsIgnoreCase("HELL") == true
assert text.containsIgnoreCase("OLD") == false
assert text.containsIgnoreCase("OrLd") == true

Conclusion

Here are a few new interesting methods added to Groovy in the 3.0 release. The list of all changes is much more impressive, so please take a look at the release notes. You can expect some more Groovy 3 related content soon on my blog, as well as on my YouTube channel. See you next time!


1. February 10th, 2020

Thank you!

If you read up to this point - leave me a comment below. It means a lot to me to hear your opinion. Share this blog post on Twitter, Facebook, or LinkedIn, if you found it helpful.

I will be very grateful if you consider supporting my work and effort. I run this blog to share my experience, inspire, and help other people to become better problem solvers.

Szymon Stepniak

25,83395888

Groovista, Upwork's Top Rated freelancer, Toruń Java User Group founder, open source contributor, Stack Overflow addict, bedroom guitar player. I walk through e.printStackTrace() so you don't have to.