天天看点

string中find()和substr()的用法

string中find()和substr()的用法

查找从指定位置开始的

string s="123453";
cout<<s.find('3')<<endl;
cout<<s.find('3',2);      
输出:
2
2      

当找不到的时候,函数会返回一个​

​s.npos​

找第一个目标字符串的位置和最后一个的位置(不是全匹配):

string s="123453";
cout<<s.find_first_of("34")<<endl;
cout<<s.find_last_of("54")<<endl;      
输出:
2
4      

在使用​

​substr(pos,n)​

​函数的时候一定要注意里面的参数,第一个是起点,第二个是长度!