HomeAboutArchivesTools & ResourcesSupport me
Hi! 👋 I'm Szymon

I help you become a better software developer.

  • 1.15kFollowers
  • 2.36kSubscribers
  • 110Followers
  • 30.2kReputation

Latest posts from the Groovy Cookbook

Groovy Ecosystem Usage Report (2020)
How to merge two maps in Groovy?
Groovy dynamic Maps, generic type erasure, and raw types - an...
Groovy 3 @NullCheck annotation - less code and less NPE
Groovy 3 String GDK improvements - takeRight, takeBetween, an...
Three Groovy String methods that will make your life Groovier!
Quicksort in Groovy - can it be as fast as implemented in Java?
More

Popular categories

  • Groovy Cookbook28
  • Jenkins Pipeline Cookbook9
  • Programmer's Bookshelf6
  • Micronaut Cookbook4
  • Ratpack Cookbook4
  • Learning Java4
  • jq cookbook3
  • How to3
  • Blog Reports2

Don't miss anything - join my newsletter

Additional materials and updates for subscribers. No spam guaranteed.
Unsubscribe at any time.

Did you like this article?

Spread the !
  • ☕️
  1. e.printstacktrace.blog
  2. Groovy Cookbook

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

  • February 12, 2020
  • 3 min. read
  • 0 Comments

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.

Table of Contents
  • String.takeRight(int)
  • String.takeAfter(str)
  • String.takeBefore(str)
  • String.takeBetween(from,to)
  • String.dropRight(int)
  • String.startsWithIgnoreCase(str) and similar
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

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


1. February 10th, 2020
  • groovy
  • string
  • groovy-3

Groovy Cookbook

Groovy 3 @NullCheck annotation - less code and less NPE

  • February 14, 2020
  • 3 min. read
  • 0 Comments

Groovy 3 helps you write less, but more secure code. Today I want to show you one of the features added in the latest release - @NullCheck annotation.

Groovy Cookbook

Groovy: split string and avoid getting IndexOutOfBoundsException

  • June 30, 2018
  • 3 min. read
  • 0 Comments

If you use Groovy for scripting or other similar tasks you probably faced a situation where you ...

Groovy Cookbook

Three Groovy String methods that will make your life Groovier!

  • December 18, 2019
  • 3 min. read
  • 0 Comments

Groovy String API offers many useful methods to make working with strings much more pleasant. To...

Groovy Cookbook

Groovy 3 @NullCheck annotation - less code and less NPE

  • February 14, 2020
  • 3 min. read
  • 0 Comments

Groovy 3 helps you write less, but more secure code. Today I want to show you one of the feature...

Any thoughts or ideas?

Let's talk in the comment's section 💬

Want to put a code sample in the comment? Read the Syntax highlighting guide for more information.
Empty
Latest blog posts
  • Merging JSON files recursively in the command-line
  • Jenkins Declarative Pipeline with the dynamic agent - how to configure it?
  • Groovy Ecosystem Usage Report (2020)
  • How to convert JSON to CSV from the command-line?
  • 5 Common Jenkins Pipeline Mistakes
  • How to merge two maps in Groovy?
  • Building stackoverflow-cli with Java 11, Micronaut, Picocli, and GraalVM
  • How to catch curl response in Jenkins Pipeline?
  • Atomic Habits - book review
  • Parsing JSON in command-line with jq: basic filters and functions (part 1)
Trending videos
Jenkins Declarative Pipeline vs Scripted Pipeline - 4 key differences | #jenkinspipeline

Jenkins Pipeline as a code is a new standard for defining continuous integration and delivery pipelines in Jenkins. The scrip...

5 Common Jenkins Pipeline Mistakes 🔥

In this Jenkins Pipeline tutorial video, I reveal five common Jenkins Pipeline mistakes and I explain how those mistakes can ...

Useful links
  • Start here
  • About
  • Archives
  • Resources
  • Privacy Policy
  • Merchandise
  • My Kit
  • RSS
  • Support the blog
Popular categories
  • Groovy Cookbook28
  • Jenkins Pipeline Cookbook9
  • Programmer's Bookshelf6
  • Micronaut Cookbook4
  • Ratpack Cookbook4
  • Learning Java4
  • jq cookbook3
  • How to3
  • Blog Reports2
Popular tags
affiliate async benchmark blogging book career cicd continuous-integration curl devops docker git github graalvm gradle grails groovy haskell hexo java java-8 jenkins jenkins-pipeline jenkinsfile jmh jq json junit learning maven metaprogramming micronaut native-image non-blocking progress ratpack reactive-programming reading recursion review rxjava sdkman session split spock stackoverflow string tail-call tail-recursion unit-test
  • Designed by @wololock
  • Created with Hexo
  • Hosted on GitHub
  • Deployed with Circle CI
  • License CC BY-NC-SA 4.0