C++20 sections · 1024 units
Open in Course

Finding in Strings

Locating text

Search for text with s.find(target). Returns the index where target first appears, or string::npos if not found. Target can be a string or single character. Find starts from beginning by default.

Add second parameter to start elsewhere: s.find("x", 5) searches from index 5. Use this to find multiple occurrences. Search is case-sensitive. Looking for "Hello" won't find "hello".

For case-insensitive search, convert both to same case first. Find performs exact matching.