Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Do you know how we can find a character or word in a string? Do you know that string__npos in C++ helps in finding a character or word in a string?
In this article, we will learn about the string__npos in C++. We will also see a coded example using the string::npos method to find whether one string is contained inside another.
What is string__npos in C++?
It is a static member constant value with the maximum possible value for an element of value size_t. The static keyword helps reserve the memory, while the constant keyword makes it a read-only variable, i.e., its value can not be changed. It means "until the end of the string" when this value is used as the value for a len (length) parameter in the string's member functions. The constant is defined with a value of -1 as size_t is an unsigned integral type value which is the maximum representable value for this type.
In simple words, npos is similar to no-position, whose return value indicates that no matches were found in the string. Therefore, if a true value is returned, it means that the matches are found at no position.
Syntax
static const size_t npos = -1;
You can also try this code with Online C++ Compiler
The above code helps in finding the matching records from a string. It checks the string from the 0th index till the end of the string. The second statement is a conditional statement that, if it holds true, i.e., if matches are found, then goes inside the condition’s body, while if it is false, then the compiler executes the next statement.
*Note: The first statement is already fed/present in the compiler. We don’t have to write it. It is just written for representation in the above code.
Library
#include <bits/stdc++.h>
You can also try this code with Online C++ Compiler
By including the above library in the code, we can use string::npos. string::npos can be used without including "iostream" and is used to find the input and the output. We find the length of the string and then compare the string with other strings.
Example
The below program finds the string s1 in string s2 using the “String::Npos” Method
Implementation
C++
C++
// Library file #include <bits/stdc++.h> using namespace std;
// Function to check the index of the occurrence // of any string in the given string using // string::npos void check(string s1, string s2) { // Finding position of string s2 int pos = s1.find(s2);
// Condition Check if pos is -1 or not if (pos != string::npos) { cout<<s2<<" found at index "<<pos<<" in the string "<<s1<<endl; }
else { cout<<s2<<" is not present in the string "<<s1<<endl; }
The check function in the above code helps in finding the matching strings. Using the .find() method, we find whether string s1 contains s2 or not. If string s1 contains s2, it returns the index in s1 where s2 is found, or it has the value -1. Using the conditional statement, we check whether the variable "pos" is equal to string::npos or not. If pos is -1 (i.e., no match is found), the else block statements are executed as the value of string::npos is -1. Otherwise, the statements inside the if block are executed.
Uses of string__npos
Some of the uses of string__npos in C++ are as follows:
Substrings Search
The value of string__npos helps in indicating whether a substring is present inside a string or not. As seen in the above example, “Ninjas” is a substring of “Coding Ninjas” while “Be a Ninja” is not.
Character Existence
string__npos helps in finding whether a character is present in a string or not. The find() function helps in finding the position of the first occurrence of the character (if it exists, else it returns -1). For example,
Implementation
C++
C++
#include <bits/stdc++.h> using namespace std;
// Code to find the index of the occurrence // of any character in the given string using // string::npos int main() { // Defining string s string s = "Coding Ninjas"; char character='i'; int a=s.find(character);
if(a==string::npos) { cout<<"Character '"<<character<<"' is not present in "<<s<<endl; } else { cout<<"Character '"<<character<<"' is present at index "<<a<<endl; } cout<<endl; return 0; }
You can also try this code with Online C++ Compiler
The string__npos helps in extracting substring from a bigger substring with the help of substr() function. The substr() function takes the starting position and the length as arguments. If we want to extract a substring from the starting position of a string till the end of the string, we can pass string__npos as the length argument. For example,
Implementation
C++
C++
#include <bits/stdc++.h> using namespace std;
// Code to extract substring // from a given string with // string::npos int main() { // Defining string s string s = "Coding Ninjas"; string sub=s.substr(7,string::npos);
//Substring from 7th position till the end of the string cout<<sub<<endl; return 0; }
You can also try this code with Online C++ Compiler
size_t is an unsigned integral data type representing objects' size in bytes. It is widely used in various libraries to represent counts and sizes. The size_t data type is never negative.
What is the value and use of string::npos in C++?
string::npos is a static member with a constant value of -1. npos is similar to no-position, whose return value indicates no matches were found in the string. Therefore, if a true value is returned, it means that the matches are found at no position.
What does string :: NPOS include?
std::string::npos is a constant in the C++ Standard Library that represents the maximum possible value for an index in a string. It is used to indicate that a substring or character was not found within the string.
Conclusion
In this article, we have discussed the string::npos method in C++. To learn more about string functions, refer to the article below. We hope this article has helped you understand the string::npos method in C++. If this article helped you in any way, then you can read more such articles on our platform,
Live masterclass
Zero to GenAI Developer: Amazon SDE Roadmap for 30L+ CTC
by Sumit Shukla
31 Mar, 2026
03:00 PM
Python + AI Skills to ace 30L+ CTC Data roles at Amazon
by Prerita Agarwal
29 Mar, 2026
06:30 AM
Zero to Data Analyst: Google Analyst Roadmap for 30L+ CTC
by Prashant
30 Mar, 2026
01:30 PM
Get hired with 25L+ CTC Interview-ready GenAI project @Amazon
by Anubhav Sinha
30 Mar, 2026
03:00 PM
Beginner to GenAI Engineer Roadmap for 30L+ CTC at Amazon
by Shantanu Shubham
29 Mar, 2026
08:30 AM
PowerBI + AI for Data Analytics: Secure 30L+ CTC at Netflix
by Ashwin Goyal
31 Mar, 2026
01:30 PM
Zero to GenAI Developer: Amazon SDE Roadmap for 30L+ CTC
by Sumit Shukla
31 Mar, 2026
03:00 PM
Python + AI Skills to ace 30L+ CTC Data roles at Amazon