How to check whether a string contains a substring in Ruby
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Substring checks are one of the most common string operations in Ruby. The main question is not whether Ruby can do it, but which method expresses your intent most clearly for a given case.
For simple presence checks, include? is usually the right answer. When you also need the position, pattern matching, or case-insensitive rules, other methods are better.
Use include? for Straightforward Checks
If you only need a true or false answer, String#include? is the clearest option.
Output:
This reads well and avoids the extra mental overhead of regular expressions when you are matching a literal substring.
Use index When Position Matters
Sometimes presence is not enough. You may want the offset of the first match so you can slice the string or highlight a UI fragment.
index returns an integer or nil. In Ruby, 0 is truthy, so the first-character case still works correctly in an if statement.
Use Regular Expressions for Patterns
If the question is more complex than a literal substring, use a regular expression with match?.
match? is useful when you want a boolean result and do not need the matched text. It avoids creating a MatchData object, so it is a good fit for validation-style checks.
Case-Insensitive Checks
All of the literal methods are case-sensitive. If your rule should ignore letter case, normalize both sides or use a case-insensitive regular expression.
Normalizing with downcase is simple for literal matching. A regex with the i flag is often nicer when you already need pattern logic.
Choosing the Right Method
A practical rule of thumb looks like this:
- use
include?for a literal yes-or-no check - use
indexwhen you also need the location - use
match?for patterns or flags such as case-insensitive matching
Here is a small comparison script:
The bracket form can also search for a substring, but it is less explicit than include? when your real goal is a boolean check. Most Ruby codebases prefer the more direct method name.
Encoding and Unicode Notes
Ruby strings carry encoding information. In normal application code, substring checks work well as long as both strings use compatible encodings.
If text arrives from multiple systems, encoding mismatches can produce surprising behavior. Normalize the data flow early, especially when you read files, parse HTTP payloads, or combine user input with stored content.
For human text, substring logic also does not automatically solve every Unicode normalization issue. Two strings that look the same can still have different internal representations. If exact multilingual matching matters, normalize input before searching.
Common Pitfalls
- Using a regular expression for a fixed literal substring when
include?is simpler and easier to read. - Forgetting that Ruby substring searches are case-sensitive unless you normalize the text or use a regex flag.
- Using
=~for boolean logic and then mishandling the numeric return value.match?is clearer when you wanttrueorfalse. - Assuming
String#[]is the best presence-check API. It works, but it hides intent compared withinclude?orindex. - Ignoring encoding and normalization issues when strings come from different external systems.
Summary
- '
include?is the best default for checking whether a literal substring exists.' - '
indexis useful when you need the match position.' - '
match?is the right tool for pattern-based or case-insensitive checks.' - Be explicit about case sensitivity instead of relying on assumptions.
- Prefer the method that matches the actual question your code is trying to answer.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.