C++ std::string contains() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::contains() returns true if a substring or character appears anywhere in the string (C++20).

It is the readable form of find(...) != npos. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Pass text or a character.

C++
bool ok = s.contains("C++");
bool has = s.contains('@');
CallWhat it does
s.contains(str)True if str occurs as a substring.
s.contains(ch)True if character ch occurs.

Needs C++20. On older modes, write s.find(x) != std::string::npos.

Library declarations
C++
bool contains(const string& str) const noexcept; // C++20
bool contains(char ch) const noexcept;
bool contains(const char* s) const;

Parameters

ParameterDescription
needleSubstring or character to look for.

Return Value

Return ValueDescription
booltrue if found, otherwise false.

C++ Examples

Example 1 — Basic contains()

Check for a substring.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Hello C++";
    std::cout << std::boolalpha << s.contains("C++") << '\\n';
    return 0;
}

How It Works

  1. The text includes "C++".
  2. contains returns true.
  3. No index is produced.

Example 2 — Contains a Character

Check for @ in an email.

C++
#include <iostream>
#include <string>

int main() {
    std::string email = "[email protected]";
    std::cout << std::boolalpha << email.contains('@') << '\\n';
    return 0;
}

How It Works

  1. Character '@' is present.
  2. contains reports true.
  3. Handy validation helper.

Example 3 — Contains False

Missing text returns false.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "abc";
    std::cout << std::boolalpha << s.contains("z") << '\\n';
    return 0;
}

How It Works

  1. No "z" substring exists.
  2. Result is false.
  3. Safe boolean—no npos pitfalls.

Frequently Asked Questions

Returns true if the string contains a given substring or character.
C++20.
s.find(x) != std::string::npos.
No—only bool. Use find when you need the position.
Yes.

Next: copy()

Keep going through the string functions. Copy characters into a caller-provided char buffer (no added null).

copy() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful