C++ substr

时间:2023-01-11 17:37:12

函数文档:http://www.cplusplus.com/reference/string/string/substr/

/*
  substr(size_t pos = 0,size_t len = npos)    子字符串是从字符位置pos开始并跨越len字符(或直到字符串结尾,以先到者为准)的对象部分。
*/

eg:

 #include<bits/stdc++.h>
using namespace std; int main() {
string str = "ABCDEFGHIJKLMN";
string str_1 = str.substr(); //从参数的位置开始一直读取到字符串结束 ABCDEFGHIJKLMN
string str_2 = str.substr(,); //从位置1开始读取长度为4的字符串 BCDE
size_t pos = str.find("EF");
string str_3 = str.substr(pos); //EFGHIJKLMN cout<<"str_1: "<<str_1<<endl;
cout<<"str_2: "<<str_2<<endl;
cout<<"str_3: "<<str_3<<endl;
return ;
}