npos in C++

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 size_t. 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 size_t 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:

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

The code mentioned above assists in extracting matched records from a string. It is checked from index 0all 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:

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:

Output:

Checking if JtP contains Java T Point
Java T Point 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 T Point". 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:

Output:

Character 't' is present at index 4

Choosing Out Substrings:

With the use of the substr() function, the string_npos aid in deriving a smaller substring from a larger substring. The starting position and length are inputs for the substr() method. We can pass string_npos as the length argument if we wish to extract a substring from a string's beginning position to its conclusion.

Example:

Output:

JTP