Andrea Ideas Back to catalog
C Program to Check the given string is Palindrome or Not
Aug 28, 2026 Andrea

Check if String Contains: A Complete Guide

When working with strings in programming, it's often necessary to check if a specific substring exists within a larger string. This operation is common in vario...

When working with strings in programming, it's often necessary to check if a specific substring exists within a larger string. This operation is common in various applications, such as data validation, string manipulation, and text analysis. In this article, we'll explore different methods to check if a string contains a specific substring in several popular programming languages.

C Program to Check the given string is Palindrome or Not

Before we dive into the code examples, let's understand why checking if a string contains another string is crucial. In many scenarios, you might want to know if a user input matches a predefined pattern, if a file path contains a specific directory, or if a text includes certain keywords. These are just a few examples where string containment checking is essential.

a computer screen with some text on it

String Containment in Python

Python provides a straightforward way to check if one string contains another using the 'in' operator or the 'find()' method.

Strings & String Methods 💻

The 'in' operator returns 'True' if the substring is found in the main string and 'False' otherwise. On the other hand, the 'find()' method returns the lowest index of the substring if found, and -1 if not found. Here's how you can use both methods:

Using the 'in' operator

a poster with the words 10 must - know c string method and other things to do

```python str1 = "Hello, world!" substring = "world" if substring in str1: print("Substring found!") else: print("Substring not found.") # Output: Substring found! ```

In this example, the 'in' operator returns 'True', indicating that the substring "world" is found in the main string "Hello, world!".

Using the 'find()' method

an image of a black screen with white text

```python str1 = "Hello, world!" substring = "world" if str1.find(substring) != -1: print("Substring found!") else: print("Substring not found.") # Output: Substring found! ```

Here, the 'find()' method returns 7 (the index of the first character of the substring "world"), so the condition is true, indicating that the substring is found in the main string.

String Containment in JavaScript

String

In JavaScript, you can use the 'includes()' method to check if a string contains another string. This method returns a boolean value, indicating whether the main string includes the specified substring or not.

```javascript let str1 = "Hello, world!"; let substring = "world"; if (str1.includes(substring)) { console.log("Substring found!"); } else { console.log("Substring not found."); } // Output: Substring found! ```

JavaScript String Methods Cheat Sheet – Search & Find (Part 1)
C program to search for a character in a string
an image of a computer screen with many lines on the screen and numbers in it
a black screen with white text on it
String Cheat Sheet for Coding Interviews | DSA String Patterns & Algorithms Guide
the menu for an old school computer game, with different colors and numbers on it
an info sheet with the words string functions on it and a man's face
a screenshot of the python string method with text and numbers in green, black and white

In this example, the 'includes()' method returns 'true', as the substring "world" is found in the main string "Hello, world!".

String Containment in Java

In Java, you can use the 'indexOf()' method or the 'contains()' method of the 'String' class to check if one string contains another. Both methods return a boolean value, indicating whether the main string includes the specified substring or not.

Using the 'indexOf()' method

```java String str1 = "Hello, world!"; String substring = "world"; if (str1.indexOf(substring) != -1) { System.out.println("Substring found!"); } else { System.out.println("Substring not found."); } // Output: Substring found! ```

Here, the 'indexOf()' method returns the index of the first character of the substring "world", so the condition is true, indicating that the substring is found in the main string.

Using the 'contains()' method

```java String str1 = "Hello, world!"; String substring = "world"; if (str1.contains(substring)) { System.out.println("Substring found!"); } else { System.out.println("Substring not found."); } // Output: Substring found! ```

In this example, the 'contains()' method returns 'true', as the substring "world" is found in the main string "Hello, world!".

Implementing a Case-Insensitive Check

In some cases, you might want to perform a case-insensitive string containment check. For example, you might want to check if a user input matches a specific pattern regardless of the case. In this section, we'll demonstrate how to implement a case-insensitive string containment check in Python and JavaScript.

Case-insensitive check in Python

```python str1 = "Hello, world!" substring = "WoRLD" if substring.lower() in str1.lower(): print("Substring found!") else: print("Substring not found.") # Output: Substring found! ```

In this example, the 'lower()' method is used to convert both the main string and the substring to lowercase before checking for containment. This ensures that the check is case-insensitive.

Case-insensitive check in JavaScript

```javascript let str1 = "Hello, world!"; let substring = "WoRLD"; if (str1.toLowerCase().includes(substring.toLowerCase())) { console.log("Substring found!"); } else { console.log("Substring not found."); } // Output: Substring found! ```

Here, the 'toLowerCase()' method is used to convert both the main string and the substring to lowercase before using the 'includes()' method. This enables a case-insensitive string containment check.

In conclusion, checking if a string contains another string is a crucial operation in programming. Depending on the programming language you're using, you have access to various methods and operators to perform this task efficiently. Understanding how to check for string containment is essential for building robust and reliable software applications. By mastering the techniques discussed in this article, you'll be well-equipped to handle string containment checks in your projects.

More shots

C Program to Check the given string is Palindrome or Not
C Program to Check the given string is Palindrome or Not
a computer screen with some text on it
a computer screen with some text on it
Strings & String Methods 💻
Strings & String Methods 💻
a poster with the words 10 must - know c string method and other things to do
a poster with the words 10 must - know c string method and other things to do
an image of a black screen with white text
an image of a black screen with white text
String
String
JavaScript String Methods Cheat Sheet – Search & Find (Part 1)
JavaScript String Methods Cheat Sheet – Search & Find (Part 1)
C program to search for a character in a string
C program to search for a character in a string

Related topics