Go deep into the usage Summary of the C ++ string. find () function

Source: Internet
Author: User

# Include <string>
# Include <iostream>
Using namespace std;
Void main ()
{
Copy codeThe Code is as follows:
//// Size_type returned by the find Function
String s ("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i ");
String flag;
String: size_type position;
// The find function returns the subscript position of jk in s.
Position = s. find ("jk ");
If (position! = S. npos) // if not found, a special sign c ++ is returned, which is represented by npos. Here, the npos value is 4294967295,
{
Cout <"position is:" <position <endl;
}
Else
{
Cout <"Not found the flag" + flag;
}

Copy codeThe Code is as follows:
// The find function returns the subscript position of any character in the flag for the first time in s.
Flag = "c ";
Position = s. find_first_of (flag );
Cout <"s. find_first_of (flag) is:" <position <endl;

Copy codeThe Code is as follows:
// Search for string B starting from string s subscript 5, and return the subscript of string B in s
Position = s. find ("B", 5 );
Cout <"s. find (B, 5) is:" <position <endl;

Copy codeThe Code is as follows:
// Find all the positions where the flag appears in s.
Flag = "";
Position = 0;
Int I = 1;
While (position = s. find_first_of (flag, position ))! = String: npos)
{
// Position = s. find_first_of (flag, position );
Cout <"position" <I <":" <position <endl;
Position ++;
I ++;
}

Copy codeThe Code is as follows:
// Find the first position in the flag that does not match s
Flag = "acb12389efgxyz789 ";
Position = flag. find_first_not_of (s );
Cout <"flag. find_first_not_of (s):" <position <endl;

Copy codeThe Code is as follows:
// Reverse lookup, the last position of the flag in s
Flag = "3 ";
Position = s. rfind (flag );
Cout <"s. rfind (flag):" <position <endl;
}

Note:
1. If string sub = "abc";
String s = "cdeabcigld";
S. find (sub), s. rfind (sub): If the two functions are completely matched, the matching index is returned. That is, the current index is returned only when s contains three consecutive letters (abc.
S. find_first_of (sub), s. find_first_not_of (sub), s. find_last_of (sub), s. the four functions find_last_not_of (sub) are used to search for indexes in s that contain any letter in sub.
2. If no query is found, string: npos is returned, which is a large number and its value does not need to be known.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.