Groovy String take() examples

Groovy is called Groovy for a reason - it is a factory of fun and intuitive way of programming. Case in point? The Groovy String take method is a method that is implemented because the name is intuitive, even though we can simulate it's behavior using the subtring method. But we explore here some samples on how to use Groovy String Take method.

According to the Groovy documentation, below is the syntax of this take method

public static CharSequence take(CharSequence self, int num)

So we can pass a number n, and the doc says that it returns a CharSequence consisting of the first num chars, or else the whole CharSequence if it has less then num elements. It is quite simple, see below example:
"James.take(3)
Will output
Jam
Because Jam is the first 3 letters of the String James. If we change the parameter:
"James.take(4)
The output will change to
Jame
Because Jame is the first 4 characters in the String James. And if num is equal to the length of String
"James.take(5)
The output will change to
James

And we are allowed to exceed when we pass the parameter. For example:

"James.take(15)

The output is still
James
Because of the documentation that says returns a CharSequence consisting of the first num chars, or else the whole CharSequence if it has less then num elements.