Logo

Programming-Idioms

History of Idiom 46 > diff from v54 to v55

Edit summary for version 55 by andyjduncan:
New Groovy implementation by user [andyjduncan]

Version 54

2020-07-18, 21:15:52

Version 55

2020-10-10, 11:59:39

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Illustration

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Illustration
Variables
t,s
Variables
t,s
Code
def t = s.take(5)
Comments bubble
Returns the entire string if shorter than five characters.
Doc URL
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/CharSequence.html#take(int)
Demo URL
https://groovyconsole.appspot.com/script/5137761203912704