Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Range contains() method



contains() method checks if a range contains a specific value.

Syntax

boolean contains(Object obj)

Parameters

obj − The value to check in the range list.

Return Value

Returns true if this Range contains the specified element.

Example - Checking a number to be present in range of numbers

Following is an example of the usage of contains() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      // range of numbers
      def array = 1..10; 
		
      println(array.contains(2)); 
      println(array.contains(11));     
   } 
}

Output

When we run the above program, we will get the following result −

true
false

Example - Checking a character to be present in range of characters

Following is an example of the usage of contains() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      // range of characters
      def array = 'a'..'z'; 
		
      println(array.contains("s")); 
      println(array.contains("S"));     
   } 
}

Output

When we run the above program, we will get the following result −

true
false

Example - Checking a string to be present in range of strings

Following is an example of the usage of contains() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      // range of characters
      def array = '&'..'!'; 
      println(array.toList())	 	
      println(array.contains('&')); 
      println(array.contains("#"));     
   } 
}

Output

When we run the above program, we will get the following result −

[&, %, $, #, ", !]
true
true
groovy_ranges.htm
Advertisements