RosettaCodeData/Task/Substring/Groovy/substring.groovy

22 lines
372 B
Groovy
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
def str = 'abcdefgh'
def n = 2
def m = 3
2019-09-12 10:33:56 -07:00
// #1
2013-04-11 01:07:29 -07:00
println str[n..n+m-1]
2019-09-12 10:33:56 -07:00
/* or */
println str[n..<(n+m)]
// #2
2013-04-11 01:07:29 -07:00
println str[n..-1]
2019-09-12 10:33:56 -07:00
// #3
2013-04-11 01:07:29 -07:00
println str[0..-2]
2019-09-12 10:33:56 -07:00
// #4
2013-04-11 01:07:29 -07:00
def index1 = str.indexOf('d')
println str[index1..index1+m-1]
2019-09-12 10:33:56 -07:00
/* or */
println str[index1..<(index1+m)]
// #5
2013-04-11 01:07:29 -07:00
def index2 = str.indexOf('de')
println str[index2..index2+m-1]
2019-09-12 10:33:56 -07:00
/* or */
println str[index2..<(index2+m)]