Groovy Cookbook

Groovy 3 String GDK improvements - takeRight, takeBetween, and a few others

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

Groovy 3 Quick Review | #groovylang
  • YouTube
  • 5k views
  • 2.5k subscribers

In this video, I check what features are added in the recent Groovy 3 release. Watch now »


1. February 10th, 2020

Did you like this article?

Consider buying me a coffee

0 Comments