Check if String Contains a Substring in Groovy

Check if String Contains a Substring in Groovy

  • Groovy
  • 3 mins read

In the realm of programming, string manipulation is a common task, and checking if a string contains a certain substring is a frequent requirement. If you're coding in Groovy, a powerful and flexible language for the Java platform, you might be wondering how to perform this task efficiently. This tutorial is designed to guide you through the process of checking if a string contains a substring in Groovy. Whether you're an experienced Groovy developer or a beginner just getting started, this guide will provide you with a clear, step-by-step approach to this common string operation. Let's delve into the world of Groovy string manipulation and learn how to effectively check for substrings.

To check if a string contains a substring in Groovy, you can use the contains() method. Here's an example:

Check if String Contains a String in Groovy Examples

This code checks if the string str contains the substring "world". If it does, it prints "str contains 'world'". If it does not, it prints "str does not contain 'world'".

def str = "Hello world"
if (str.contains("world")) {
  println "str contains 'world'"
} else {
  println "str does not contain 'world'"
}

This Groovy code is checking if the string "Hello world" contains the substring "world". Let's break it down:

  • def str = "Hello world": This line is creating a variable named str and assigning it the value "Hello world".
  • if (str.contains("world")): This line is the start of an if statement. It's checking if the str variable contains the substring "world". The contains method in Groovy is used to check if a string contains a specific sequence of characters.
  • println "str contains 'world'": If the str variable does contain the substring "world", this line will execute, and "str contains 'world'" will be printed to the console.
  • else: This keyword is used to specify a block of code to be executed if the condition in the if statement is false.
  • println "str does not contain 'world'": If the str variable does not contain the substring "world", this line will execute, and "str does not contain 'world'" will be printed to the console.

So, in simple terms, this code is saying: "If the string 'Hello world' contains 'world', tell me it does. If it doesn't, tell me it doesn't."

Here's another example that shows how to check if a string contains a substring in a case-insensitive manner:

def str = "Hello world"
if (str.toLowerCase().contains("world".toLowerCase())) {
  println "str contains 'world' (case-insensitive)"
} else {
  println "str does not contain 'world' (case-insensitive)"
}

In this code, we convert both the string and the substring to lowercase using the toLowerCase() method. This allows us to check if the string contains the substring without considering the case of the characters. If the string contains the substring, it prints "str contains 'world' (case-insensitive)". If it does not, it prints "str does not contain 'world' (case-insensitive)".

Related:

  1. Check if File Exists in Groovy
  2. Check if String is Empty in Groovy