Logo

Programming-Idioms

History of Idiom 46 > diff from v55 to v56

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

Version 55

2020-10-10, 11:59:39

Version 56

2020-10-10, 12:02:55

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[0..<5]
Comments bubble
Uses a half inclusive range. Will throw an exception if the string is shorter than 5 characters.
Doc URL
https://docs.groovy-lang.org/latest/html/api/groovy/lang/Range.html
Demo URL
https://groovyconsole.appspot.com/script/5136712170733568