C + + string string lookup match

Source: Internet
Author: User

In writing a C + + program, there is always the case of finding a small string from a string, for in C we often use either strstr () or STRCHR (). For C + + strings, we tend to use find ().

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

Find (): finds a specified single character or array of characters in a string. Returns the starting position of the first match if found, and returns String::npos if no matching content is found.
Find_first_of (): In a target string, the return value is the first character position to match any character in the specified character group. If no matching content is found, the NPOs is returned.
Find_last_of (): Finds in a target string, returning the last character position that matches any character in the specified character group. If no matching content is found, the NPOs is returned.
Find_first_not_of (): Finds in a target string and returns the position of the first element 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, returning the position of the element with the largest subscript value that does not match any of the characters in the specified character group. Returns NPOs if no such element is found.
RFind (): Finds a specified single character or group of characters on a string from tail to head. Returns the starting position of the first match if found, and returns NPOs if no matching content is found.

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

Example: string matching :

1#include"stdafx.h"2#include <iostream>3#include <math.h>4#include <string>5 using namespacestd;6 7 int_tmain (intARGC, _tchar*argv[])8 {9 stringT//Original StringTen stringP//Mode One  while(cin>>t>>P) A {  - intCount=0; - intbegin=-1; the  while((Begin=t.find (p,begin+1))!=string:: NPOs) - { -count++; - } +cout<<count<<Endl; - } + intZ; ACin>>Z; at return 0; -}

Reprint please indicate source: http://www.cnblogs.com/fnlingnzb-learner/p/5831454.html

C + + string string lookup match

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.