Programming
String Manipulation
Substring
Coding Tutorial
Duplicate Content

How do I check if string contains substring?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Checking whether one string contains another is one of the most common string operations in programming. The exact syntax depends on the language, but the underlying question is the same: does a contiguous sequence of characters appear inside the larger string.

The Core Idea

A substring check is different from:

  • equality
  • prefix checking
  • suffix checking
  • regular-expression matching

If you only need to know whether "world" appears anywhere inside "hello world", use the simplest built-in operation your language provides.

Python

In Python, the clearest option is the in operator.

python
1text = "hello world"
2needle = "world"
3
4if needle in text:
5    print("found")
6else:
7    print("not found")

If you also need the index, use find:

python
1text = "hello world"
2needle = "world"
3
4index = text.find(needle)
5print(index)   # 6

find returns -1 when the substring is absent.

Java

Java uses String.contains.

java
1public class Main {
2    public static void main(String[] args) {
3        String text = "hello world";
4        String needle = "world";
5
6        if (text.contains(needle)) {
7            System.out.println("found");
8        } else {
9            System.out.println("not found");
10        }
11    }
12}

If you need the position, indexOf is the usual alternative:

java
int index = text.indexOf(needle);
System.out.println(index);  // 6

JavaScript

In JavaScript, use includes for the boolean check.

javascript
1const text = "hello world";
2const needle = "world";
3
4console.log(text.includes(needle));

If you need the position:

javascript
console.log(text.indexOf("world"));  // 6

Case Sensitivity

Most default substring checks are case-sensitive. That means "World" is not the same as "world".

Example in Python:

python
1text = "Hello World"
2needle = "world"
3
4print(needle in text)                 # False
5print(needle.lower() in text.lower()) # True

The same normalization idea applies in other languages:

  • Python: lower() or casefold()
  • Java: toLowerCase()
  • JavaScript: toLowerCase()

Use this only when case-insensitive matching is actually intended.

When a Regex Is Overkill

Many developers reach for regular expressions too early. If you only want a plain substring check, regex is usually unnecessary and less readable.

Plain substring search:

javascript
text.includes("world")

Regex:

javascript
/world/.test(text)

The regex version is only worth it when you need pattern rules rather than a literal substring.

Empty Strings and Edge Cases

Most languages treat the empty string as being contained in every string.

Examples:

  • '"abc".contains("") in Java is true'
  • '"" in "abc" in Python is true'

That behavior is correct according to common string definitions, but it surprises people if they are not expecting it.

You should also think about:

  • case sensitivity
  • Unicode normalization
  • null or None values
  • whether you need the first index or just a yes/no answer

Null Safety

A substring check on a missing string often throws an error.

Java example:

java
1String text = null;
2String needle = "world";
3
4if (text != null && text.contains(needle)) {
5    System.out.println("found");
6}

Do not assume every string variable actually contains a string.

Common Pitfalls

Using regex when a plain substring check is enough makes code harder to read and easier to misconfigure.

Forgetting that the default check is case-sensitive causes many false negatives.

Confusing "contains substring" with "starts with" or "ends with" leads to the wrong built-in method.

Ignoring null handling can turn a simple check into a runtime error, especially in Java or C#.

Summary

  • Use the language's built-in substring check first: in, contains, or includes.
  • Use find or indexOf when you need the position as well.
  • Remember that most substring checks are case-sensitive by default.
  • Avoid regex unless you need pattern matching rather than literal matching.
  • Handle null or missing values explicitly in languages where string methods can throw.

Course illustration
Course illustration

All Rights Reserved.