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.
If you also need the index, use find:
find returns -1 when the substring is absent.
Java
Java uses String.contains.
If you need the position, indexOf is the usual alternative:
JavaScript
In JavaScript, use includes for the boolean check.
If you need the position:
Case Sensitivity
Most default substring checks are case-sensitive. That means "World" is not the same as "world".
Example in Python:
The same normalization idea applies in other languages:
- Python:
lower()orcasefold() - 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:
Regex:
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
Nonevalues - 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:
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, orincludes. - Use
findorindexOfwhen 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.

