Logo

Programming-Idioms

History of Idiom 38 > diff from v57 to v58

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

Version 57

2020-05-10, 21:58:49

Version 58

2020-10-10, 12:12:19

Idiom #38 Extract a substring

Find substring t consisting in characters i (included) to j (excluded) of string s.
(character indexes start at 0 unless specified otherwise)

Illustration

Idiom #38 Extract a substring

Find substring t consisting in characters i (included) to j (excluded) of string s.
(character indexes start at 0 unless specified otherwise)

Illustration
Variables
t,i,j,s
Variables
t,i,j,s
Code
def t = s[i..<j]
Comments bubble
Uses a half-inclusive range. Throws a StringIndexOutOfBoundsException if either index extends beyond the end of the string. Indicies can be negative, in which case they count from the end of the string.
Doc URL
https://docs.groovy-lang.org/latest/html/api/groovy/lang/Range.html
Demo URL
https://groovyconsole.appspot.com/script/5167058048253952