Slice a string in groovy

Mikey picture Mikey · May 14, 2011 · Viewed 25.9k times · Source

I have a 18 character string I want characters 2-8 from. In python I can do this:

sliceMe = "nnYYYYYYnnnnnnnnnn"
print sliceMe[2:8]

prints

YYYYYY

I am looking for a way to do this same thing in groovy, and every explanation is REALLY long. Whats the elegant accepted way to do this in groovy (or java for that matter)?

Answer

onteria_ picture onteria_ · May 14, 2011
groovy:000> sliceMe = "nnYYYYYYnnnnnnnnnn"
===> nnYYYYYYnnnnnnnnnn
groovy:000> sliceMe[2..7]
===> YYYYYY

Note the difference in the length being 1 less.