In this article, we will study the string__npos in C++. We'll also see a programmed example of how to determine whether one string is contained inside another using the string::npos method .
What is string_npos?
The npos is the constant static member value with the maximum value for an element of type sizet . The static keyword aids with memory reservation , while the constant keyword makes it a read-only variable , meaning its value cannot be modified . When the string's member functions use this number as the value for a len (length) parameter , it indicates "until the end of the string" . The value of the constant is declared as -1 since sizet is an unsigned integral type value, which is the highest number that can be represented for this type.
In plain English, npos is comparable to no-position , whose return value shows that no matches were discovered in the string. As a result, if a true value is returned, it indicates that no matches were identified.
Syntax:
It has the following syntax:
static constsize_tnpos = -1;
In this case, the static constant value has the largest value of the npos that size_t can take. It has the value -1 when it is defined or stored.
Case study
static constsize_tnpos=-1;
variable != string::npos
The code mentioned above assists in extracting matched records from a string. It is checked from index 0 all the way to the end of the string. The second statement is conditional ; if it is true , the compiler executes the next statement. If the second statement is false , the compiler executes the next statement.
Library included:
#include <bits/stdc++.h>
We can utilize string::npos by incorporating the aforementioned library in the code. It is possible to find the input and output by using string::npos without including "iostream" . We measure the string's length before comparing it to other strings.
Example:
Using the "String::Npos" method, the program below locates the string s1 within the string s2 .
Code:
#include <bits/stdc++.h>
using namespace std;
// Using the string::npos function, verify the index of any string's appearance in the input string.
void check(string s1, string s2)
{
// Positioning the string s2
int pos = s1.find(s2);
// Verify whether or not pos is -1.
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;
}
}
int main()
{
// here we have defined the strings
string s1 = "JtP";
string s2 = "Java Cpp Tutorial";
string s3 = "JtP";
// Function call on string s1 and s2
cout<<"Checking if "<<s1<<" contains "<<s2<<endl;
check(s1, s2);
cout<<endl;
cout<<"Checking if "<<s1<<" contains "<<s3<<endl;
check(s1, s3);
cout<<endl;
return 0;
}
Output:
Checking if JtP contains Java Cpp Tutorial
Java Cpp Tutorial is not present in the string JtP
Checking if JtP contains JtP
JtP found at index 0 in the string JtP
Explanation:
The first function call, check (s1, s2) , determines whether the main string "JtP" has the substring "Java Cpp Tutorial" . Given that s1 does not contain this substring .
The second function call, check (s1, s3) ; verifies whether the substring "JtP" is contained within the main string "JtP" , because s1's starting index is where this substring is located.
Uses of String__npos:
Following are some examples that demonstrate how string__npos is used in C++:
Find Substrings:
The string_npos value can be used to determine whether a substring is contained within a string or not .
Character Existence:
Use the string_npos to determine whether a character is present in a string or not. The locate function aids in locating the first instance of a character (if it occurs; otherwise, it returns -1 ).
Example:
#include <bits/stdc++.h>
using namespace std;
// Code to use string::npos to retrieve the index of any character in the given string.
int main()
{
string s = "javacpptutorial";
char character='t';
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;
}
Output:
Character 't' is present at index 4
Choosing Out Substrings:
With the use of the substr function , the stringnpos aid in deriving a smaller substring from a larger substring . The starting position and length are inputs for the substr method . We can pass stringnpos as the length argument if we wish to extract a substring from a string's beginning position to its conclusion.
Example:
#include <bits/stdc++.h>
using namespace std;
// Code using string::npos to retrieve a substring from a supplied string
int main()
{
// Defining string s
string s = "Javacpptutorial JTP";
string sub=s.substr(10,string::npos);
//Substring from the 10th position till the end of the string
cout<<sub<<endl;
return 0;
}
Output: