Summary of usage of string.find () function in C + + (reproduced)

Source: Internet
Author: User

#include <stdio.h>

#include <iostream>

using namespace Std;

void Main ()

{

Find function return type Size_type
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, return a special flag in C + + with the NPOs representation, I'm here NPOs value is 4294967295,
{
cout << "position is:" << position << Endl;
}
Else
{
cout << "Not found the flag" + flag;
}

The Find function returns the subscript position of any character in flag for the first occurrence in S
Flag = "C";
Position = s.find_first_of (flag);
cout << "s.find_first_of (flag) is:" << position << Endl;

Starting with the string s subscript 5, find the string B and return the subscript of B in S
Position=s.find ("B", 5);
cout<< "S.find (b,5) is:" <<position<<endl;

Finds all locations in s where flag appears.
Flag= "a";
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++;
}

Find the position in flag that does not match s first
Flag= "acb12389efgxyz789";
Position=flag.find_first_not_of (s);
cout<< "Flag.find_first_not_of (s):" <<position<<endl;

Reverse lookup, where flag appears last in S
Flag= "3";
Position=s.rfind (flag);
cout<< "S.rfind (flag):" <<position<<endl;
}

Description

1. If the string sub = "abc";

string s = "CDEABCIGLD";

S.find (sub), S.rfind (sub) These two functions, if they match exactly, return the matching index, that is, when s contains ABC three consecutive letters, the current index is returned.

S.find_first_of (sub), s.find_first_not_of (sub), s.find_last_of (sub), s.find_last_not_of (sub) These four functions, Finds an index in S that contains any of the letters in a sub.

2. If no query is reached, the String::npos is returned, which is a large number and its value does not need to be known.

Summary of usage of string.find () function in C + + (reproduced)

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.