C + + String string lookup match instance Code _c language

Source: Internet
Author: User

In writing C + + programs, there is always a case where you want to look up a small string from a string, and for C, we often use either strstr () or STRCHR (). and for C + + string, we tend to use find ().

C + +: #inlcude <string>
C: #include <string.h>

Find (): finds a single specified character or character array in a string. If found, returns the start position of the first match, and returns String::npos if no matching content is found.

find_first_of (): finds in a target string that is the first character position to match any character in the specified set of characters. Returns NPOs if no matching content is found.

find_last_of (): finds in a target string, returns the last character position that matches any character in the specified set of characters. Returns NPOs if no matching content is found.

find_first_not_of (): finds in a target string, returning the first element position that does not match any of the characters in the specified character group. Returns NPOs if no such element is found.

find_last_not_of (): finds in a target string that returns the position of the largest element that does not match any character in the specified set of characters. If no such element is found, return to NPOs.

RFind (): Finds a single specified character or group of characters for a string from the end to the header. If found, returns the starting position of the first match, or NPOs if no match is found.

Find (string, int): The first parameter is used to indicate the character to look for, and the second argument is used to indicate where to start looking for substrings from the string (the default lookup location is 0).

Example: string matching:

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int _tmain (int argc, _tchar* argv[])
{
string t;//original string
string p;//mode while
(CIN>>T>>P)
{ 
int count=0;
int begin=-1;
while ((Begin=t.find (p,begin+1))!=string::npos)
{
count++;
}
cout<<count<<endl;
}
int Z;
cin>>z;
return 0;
}

The above is a small series for everyone to bring the C + + string string to find the matching instance code all content, I hope that we support cloud-Habitat Community ~

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.