一、作用
用來截取某段字符串。
二、頭文件
#include<string>
三、參數與用法
形式:s.substr(pos, len)
第一個參數是想要截取的字符串初始位置,第二個參數是截取字符串長度。
直接來說,就是從s[pos]開始截一個長度為len的子串。
注:如果len超出了s的長度,就一直會從s[pos]截取到最后。
四、舉例
代碼:
#include <iostream>
#include <string>using namespace std;int main()
{string s="I'm a good girl!So follow me!";string s1=s.substr(4,11);string s2=s.substr(4,100);cout<<"s1為:"<<s1<<endl;cout<<"s2為:"<<s2<<endl;
}
運行結果:
s1為:a good girl
s2為:a good girl!So follow me!