std::basic_string::substr
函数定义及功能描述
注: 默认参数npos means "until the end of the string"
示例程序
#include<iostream>
#include <string>
using namespace std;
int main()
{
string t = "15:30:28";
// 1 5 : 3 0 : 2 8
// 0 1 2 3 4 5 6 7
string h = t.substr(0, 2);
string m = t.substr(3, 2);
string s = t.substr(6);
cout << h << ' ' << m << ' ' << s << endl;
return 0;
}
std::basic_string::find
函数定义及功能描述
注:
- When pos is specified, the search only includes characters at or after position pos,注意当提供pos参数时,是包含pos那个位置的
- 可以 Find position of a character 也可以 Find position of a C string.
- If not found, returns npos,即-1
实例程序
#include<iostream>
#include <string>
using namespace std;
int main()
{
string t = "15:30:28";
// 1 5 : 3 0 : 2 8
// 0 1 2 3 4 5 6 7
int pos1 = t.find(':');
int pos2 = t.find(':', pos1 + 1);
cout << pos1 << ' ' << pos2 << endl;
return 0;
}
std::basic_string::rfind
函数定义及功能描述
注:
- When pos is specified, the search only includes sequences of characters that begin at or before position pos, rfind是倒着找,仅考虑pos及以前的位置
- 可以 Find position of a character 也可以 Find position of a C string.
- If not found, returns npos,即-1
实例程序
#include<iostream>
#include <string>
using namespace std;
int main()
{
string t = "15:30:28";
// 1 5 : 3 0 : 2 8
// 0 1 2 3 4 5 6 7
int pos1 = t.rfind(':');
int pos2 = t.rfind(':', pos1 - 1);
cout << pos1 << ' ' << pos2 << endl;
return 0;
}