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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field
  • Swift: Master of Decoding Messy JSON
  • Secrets in Code: Understanding Secret Detection and Its Blind Spots

Trending

  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik
  • Kafka With Spring Boot: Introduction
  • What Cloud Engineers Actually Need to Know About AI Infrastructure

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jun. 08, 18 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Replacing JSON With Protobuf in Your Microservice Mesh: A Zero-Downtime Migration Blueprint
  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field
  • Swift: Master of Decoding Messy JSON
  • Secrets in Code: Understanding Secret Detection and Its Blind Spots

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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