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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Learning Kotlin: Looking at the In Operator and Contains

Learning Kotlin: Looking at the In Operator and Contains

Want to learn more about using the in operator and contains in Kotlin? Check out this post to learn more and check out this sample code!

Robert Maclean user avatar by
Robert Maclean
·
Sep. 14, 18 · Tutorial
Like (7)
Save
Tweet
Share
11.94K Views

Join the DZone community and get the full member experience.

Join For Free

This is the 22nd post of this multipart series on learning Kotlin. If you want to read more, see our series index. The code for this can be found here.

One of my absolute favorite features in Kotlin are ranges. You can easily go 1..10 to get a range of numbers from one to ten. In addition, so much of the way I find I want to work with Kotlin is around working with collections, like lists and arrays.

With all of those, we often want to know when something exists inside the range or the collection, and that is where the in operator comes in. In the following example, we use the in operator to first check for a value in an array, then in a range, then a substring in a string; each example below will return true.

val letters = arrayOf("a", "b", "c", "d", "e")
println("c" in letters)

println(5 in 1..10)

println("cat" in "the cat in the hat")


Naturally, Kotlin lets us add this to our own classes, too. The example from the Koans starts with a class that represents a range of dates.

class DateRange(val start: MyDate, val endInclusive: MyDate)


We then add an operator function named contains, which checks if the value provided falls in between the two dates of the class:

class DateRange(val start: MyDate, val endInclusive: MyDate) : Iterator<MyDate> {
    operator fun contains(d: MyDate) = start <= d && d <= endInclusive
}


With this new function, we can write our own in statement, for example:

fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {
   return date in DateRange(first, last)
}


Kotlin (programming language) Operator (extension)

Published at DZone with permission of Robert Maclean, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Memory Debugging: A Deep Level of Insight
  • How and Why You Should Start Automating DevOps
  • Using JSON Web Encryption (JWE)
  • Top 12 Technical Skills Every Software Tester Must Have

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: