Article Directory
1 Introduction
A. Function prototype:
return the substring above the position [ pos, pos + count ) on the string string
2. Analysis
A. Header file: iostream
B. Use detailed analysis:
string s = "abcdef";
1. If both parameters are omitted, the entire string will be returned by default
cout << s.substr() << endl; // abcdef
2. If the second parameter is omitted, the default value of the second parameter is the string size() - pos (default to the end of the string)
cout << s.substr(2) << endl; // cdef
3. Out of bounds processing The first parameter out of bounds (too small/too large) will report an error, and the second parameter out of bounds will directly default to the end of the string
cout << s.substr(-1) << ' ' << s.substr(7) << endl; // 抛出错误异常
cout << s.substr(2, 100) <<endl; // cdef
3. use
#include<iostream>
#include<cstring>
using namespace std;
int main(){
string s = "abcdef";
cout << s.substr() << endl; // abcdef
cout << s.substr(2) << endl; // cdef
// cout << s.substr(-1) << ' ' << s.substr(7) << endl; // 抛出错误异常
cout << s.substr(2, 100) <<endl; // cdef
}