In this guide, we will explore the string::npos in C++. Additionally, we will demonstrate a coding example showcasing how to ascertain if a string is present within another using the string::npos method.
What is string_npos?
The ```
static constsize_tnpos = -1;
In simple terms, npos can be likened to no-position, where its output indicates that there were no findings within the string. Therefore, a positive return value signifies that no matches were detected.
### Syntax:
It has the following syntax:
static constsize_tnpos = -1;
In this scenario, the static constant value represents the maximum value that can be held by size_t. It is set to -1 upon definition or storage.
### Case study
static constsize_tnpos=-1;
variable != string::npos
The provided code aids in retrieving matching records from a string by examining it starting from index 0 up to the string's end. The subsequent condition determines the execution flow: if the condition evaluates to true, the compiler proceeds with the following statement; otherwise, the compiler moves on to the subsequent statement.
### Library included:
include <bits/stdc++.h>
We can make use of string::npos by integrating the mentioned library in the code. It is feasible to locate the input and output by utilizing string::npos without the need to include "iostream". The length of the string is evaluated prior to comparing it with other strings.
### Example:
By employing the "String::Npos" function, the following code snippet identifies the position of string s1 within 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 initial function call, verify(s1, s2), checks if the primary string "JtP" contains the sub-string "Java Cpp Tutorial". It is assumed that s1 does not include this specific sub-string.
The subsequent function invocation, verify (s1, s3) ; examines whether the sub-string "JtP" exists within the primary string "JtP" , as the initial index of s1 corresponds to the position of this sub-string.
### Uses of String__npos:
Here are a few instances showcasing the usage of the string::npos in C++:
### Find Substrings:
The value string_npos can be utilized to ascertain the presence of a substring within a string.
### Character Existence:
Utilize the string_npos value to ascertain the presence of a character within a string. The locate() method assists in identifying the initial occurrence of a character (returning -1 if not found).
### 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:
By employing the substr() function, the string_npos variable helps in extracting a shorter substring from a longer one. Both the initial index and the length are parameters for the substr() function. If needed, we can specify string_npos as the length parameter to extract a substring from the start to the end of a string.
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: