DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Swift: Master of Decoding Messy JSON
  • Secrets in Code: Understanding Secret Detection and Its Blind Spots
  • From HTTP to Kafka: A Custom Source Connector
  • Making String Search Easier Across Databases

Trending

  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • Stop Debugging Glue Jobs Manually: Building an Agentic Observability Layer for Data Pipelines
  • Reproducible Development Environments, One Command Away: Introducing CodingBooth
  1. DZone
  2. Coding
  3. JavaScript
  4. Groovy Goodness: Using String Values in Ranges [Snippet]

Groovy Goodness: Using String Values in Ranges [Snippet]

Want to see how to use String values in your ranges? Here's a quick lesson to set up inclusive and exclusive ranges in Groovy.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Jun. 08, 18 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
8.7K Views

Join the DZone community and get the full member experience.

Join For Free

We can use ranges in Groovy using an easy syntax where the start and end values of the range are separated by .. for an inclusive range and ..< for an exclusive range as we have seen in a previous post. The values of the range are mostly numbers or enum values. But we can also use String values to define a range. Groovy will check if the String values are the same length and if the values, except for the last character, are the same. Then the natural ordering of the last character of the String value, based on the character's int value, is used to create the range values.

In the following example, we define several ranges using String values. We can even define a reverse range using String values.

// Range is defined based on int
// value of character.
def characters = 'A'..'F'
 
assert characters.from == 'A'
assert characters.to == 'F'
assert characters.toList() == ['A', 'B', 'C', 'D', 'E', 'F']
assert characters.step(2) == ['A', 'C', 'E']
 
 
// We can create a reverse range
// also based on the int value
// of the character.
def sample = '&'..'!'
 
assert sample.toList() == ['&', '%', '$', '#', '"', '!']
assert sample.reverse
assert sample.from == '!'
assert sample.to == '&'
 
 
// We can use String values and
// the last character is used
// to create a range. Therefore
// the last character must be valid
// to create a range from.
def groovyRange = 'Groovy10'..<'Groovy15'
 
assert groovyRange.from == 'Groovy10'
assert groovyRange.to == 'Groovy14'
assert groovyRange.toList() == ['Groovy10', 'Groovy11', 'Groovy12', 'Groovy13', 'Groovy14']
 
 
// Also works in reverse.
def groovyReverse = 'Groovy19'..'Groovy15'
 
assert groovyReverse.reverse
assert groovyReverse.from == 'Groovy15'
assert groovyReverse.to == 'Groovy19'
assert groovyReverse.toList() == ['Groovy19', 'Groovy18', 'Groovy17', 'Groovy16', 'Groovy15']
 
 
import static groovy.test.GroovyAssert.shouldFail
 
// Should fail because String values,
// except for the last character, should
// be the same.
shouldFail(IllegalArgumentException) {
    def invalidRange = 'Groovy15'..'Groovy20'
}


Written with Groovy 2.5.0.

Groovy (programming language) Strings

Published at DZone with permission of Hubert Klein Ikkink. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Swift: Master of Decoding Messy JSON
  • Secrets in Code: Understanding Secret Detection and Its Blind Spots
  • From HTTP to Kafka: A Custom Source Connector
  • Making String Search Easier Across Databases

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook