子串是,在字符位置 pos 开始,跨越 len 个字符(或直到字符串的结尾,以先到者为准)对象的部分。
1 2 3
string str="We think in generalities, but we live in details."; string str2 = str.substr (3,5); // "think" string str3 = str.substr (3); // get from "think" to the end
// inserting into a string #include<iostream> #include<string>
intmain() { std::string str="to be question"; std::string str2="the "; std::string str3="or not to be"; std::string::iterator it;
// used in the same order as described above: str.insert(6,str2); // to be (the )question str.insert(6,str3,3,4); // to be (not )the question str.insert(10,"that is cool",8); // to be not (that is )the question str.insert(10,"to be "); // to be not (to be )that is the question str.insert(15,1,':'); // to be not to be(:) that is the question it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...) str.insert (it+2,str3.begin(),str3.begin()+3); // (or )