Boyer-Moore string search algorithm in C#: fast pattern matching explained
The Boyer-Moore algorithm is one of the most efficient string searching algorithms, particularly for large text and longer patterns. It searches from right to left but shifts the pattern intelligently to skip unnecessary comparisons, often running in sublinear time.
On this page
When to Use Boyer-Moore
Boyer-Moore excels when:
- Searching for patterns in large text (millions of characters)
- The pattern is relatively long (10+ characters)
- You need to find the same pattern repeatedly in different texts
- Performance is critical and you can't use native compiled solutions
For everyday string searching in C#, string.IndexOf() or string.Contains() is simpler and often faster for short patterns thanks to hardware optimizations. Use Boyer-Moore when you need the theoretical efficiency advantage or are implementing custom search logic.
How Boyer-Moore Works
Boyer-Moore uses two heuristics to skip sections of text:
- Bad Character Rule: When a mismatch occurs, shift the pattern to align the mismatched text character with its rightmost occurrence in the pattern
- Good Suffix Rule: When a mismatch occurs after matching a suffix, shift the pattern based on the matched suffix
Most implementations use just the bad character rule for simplicity. It provides most of the performance benefit.
Basic Boyer-Moore Implementation
Usage Examples
Case-Insensitive Search
Performance Comparison
Note: For most real-world C# applications, string.IndexOf() is optimized by the runtime and often faster than a managed Boyer-Moore implementation. Boyer-Moore shines in:
- Academic/educational contexts
- Custom scenarios where you need to modify the algorithm
- Very large texts with long patterns
- Platforms without native string search optimizations
Optimized Version with Span<char>
For better performance in modern C#, use Span<char> to avoid string allocations:
Real-World Use Cases
Time Complexity
-
Best case: O(n/m) where n = text length, m = pattern length
Sublinear! The algorithm can skip sections of text -
Worst case: O(n × m)
Occurs with highly repetitive patterns and text (rare in practice) -
Average case: O(n)
Much better than naive O(n × m) string matching -
Preprocessing: O(m + σ) where σ = alphabet size
Building the bad character table
Key Takeaways
✅ Use Boyer-Moore when:
- Working with very large texts
- Searching for long patterns repeatedly
- Implementing custom search algorithms
- Learning about algorithm optimization
❌ Use string.IndexOf() when:
- Doing everyday string searches
- Working with short patterns
- Performance is good enough (usually the case)
The Boyer-Moore algorithm demonstrates clever optimization: by searching right-to-left and using character occurrence information, it achieves sublinear search time in many cases, making it one of the fastest string search algorithms in practice.
Take It Further

Hand-tuned algorithms like this are just the start: learn to profile, benchmark, and squeeze allocations out of any hot path in your .NET code.
Writing High-Performance .NET Code: 2nd Edition · Ben Watson
Get it on Amazon →As an Amazon Associate I earn from qualifying purchases.
Related Articles
Learn the most commonly used C# string methods, including Contains, StartsWith, Split, Replace, Trim, Substring, IndexOf, Join, and padding examples.
Test and use C# regular expressions with examples for IsMatch, Match, Groups, named captures, Replace, validation, and common regex patterns.
Compare strings in C# correctly with ==, .Equals(), or string.Compare(). Understand StringComparison, OrdinalIgnoreCase, and CurrentCulture.
Learn C# pattern matching with is expressions, switch expressions, type patterns, property patterns, relational patterns, and list patterns.