C ++ string in the find, find_first_of and find_last_of usage

1.find

str.find(str1)

Description: front to back str1 found in str and returns its index value, otherwise -1

2.find_first_of

str.find_first_of(str1)

Description: front to back str1 found in str and returns its index value, otherwise -1

3.find_last_of

str.find_last_of(str1)

Description: Found forward from the rear in str str1, and returns its index value forward from the back, otherwise -1

#include<iostream>
using namespace std;
               
int main(void) 
{              
    string s = "一蓑烟雨任平生。";
    int len = s.size();
    int count = s.size() / string("一").size();
    cout << "len = " << len << ", count = " << count << endl;
    cout << "一: pos = " << s.find("一") << endl;
    cout << "一: pos = " << s.find_first_of("一") << endl;
    cout << "一: pos = " << s.find_last_of("一") << endl;
    int pos = s.find("一", 1);
    cout << "pos:" << pos << endl;
    cout << "。: pos = "s.find("。") << endl;                                                                                                     
    cout << "。: pos = "s.find_last_of("。") << endl;
    return 0;  
}      

result:
result:

len = 24, count = 8
a: pos = 0
a: pos = 0
a: = 22 is POS
POS: -1
0
2

to sum up:

In order to determine when the end of the string of Chinese characters find_last_of best not to use this function, error-prone. If it is judged whether or not a sentence shown in period. "" End, it is necessary to determine whether the return value as described above in Example 2, instead of the return value plus the length that the period is equal to the length of the string.

Guess you like

Origin www.cnblogs.com/zh20130424/p/11099932.html