简单,可复制

点点滴滴,尽在文中

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

初始化和赋值

// string constructor
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string s0 ("Initial string");

// constructors used in the same order as described above:
string s1;
string s2 (s0);
string s3 (s0, 8, 3);
string s4 ("A character sequence", 6);
string s5 ("Another character sequence");
string s6 (10, 'x');
string s7a (10, 42);
string s7b (s0.begin(), s0.begin()+7);

cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6: " << s6;
cout << "\ns7a: " << s7a << "\ns7b: " << s7b << endl;
return 0;
}

 

// string assigning
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str1, str2, str3;
str1 = "Test string: "; // c-string
str2 = 'x'; // single character
str3 = str1 + str2; // string

cout << str3 << endl;
return 0;
}

 

遍历所有的字符

// string::operator[]
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str ("Test string");
int i;
for (i=0; i < str.length(); i++)
{
cout << str[i];
}
return 0;
}

 

追加

// appending to string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str;
string str2="Writing ";
string str3="print 10 and then 5 more";

// used in the same order as described above:
str.append(str2); // "Writing "
str.append(str3,6,3); // "10 "
str.append("dots are cool",5); // "dots "
str.append("here: "); // "here: "
str.append(10,'.'); // ".........."
str.append(str3.begin()+8,str3.end()); // " and then 5 more"
str.append<int>(5,0x2E); // "....."

cout << str << endl;
return 0;
}

 

插入

// inserting into a string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str="to be question";
string str2="the ";
string str3="or not to be";
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 )

cout << str << endl;
return 0;
}

 

删除

// string::erase
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str ("This is an example phrase.");
string::iterator it;

// erase used in the same order as described above:
str.erase (10,8);
cout << str << endl; // "This is an phrase."

it=str.begin()+9;
str.erase (it);
cout << str << endl; // "This is a phrase."

str.erase (str.begin()+5, str.end()-7);
cout << str << endl; // "This phrase."
return 0;
}

 

替换

// replacing in a string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string base="this is a test string.";
string str2="n example";
string str3="sample phrase";
string str4="useful.";

// function versions used in the same order as described above:

// Using positions: 0123456789*123456789*12345
string str=base; // "this is a test string."
str.replace(9,5,str2); // "this is an example string."
str.replace(19,6,str3,7,6); // "this is an example phrase."
str.replace(8,10,"just all",6); // "this is just a phrase."
str.replace(8,6,"a short"); // "this is a short phrase."
str.replace(22,1,3,'!'); // "this is a short phrase!!!"

// Using iterators: 0123456789*123456789*
string::iterator it = str.begin(); // ^
str.replace(it,str.end()-3,str3); // "sample phrase!!!"
str.replace(it,it+6,"replace it",7); // "replace phrase!!!"
it+=8; // ^
str.replace(it,it+6,"is cool"); // "replace is cool!!!"
str.replace(it+4,str.end()-4,4,'o'); // "replace is cooool!!!"
it+=3; // ^
str.replace(it,str.end(),str4.begin(),str4.end());
// "replace is useful."
cout << str << endl;
return 0;
}

 

查找

// string::find
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str ("There are two needles in this haystack with needles.");
string str2 ("needle");
size_t found;

// different member versions of find in the same order as above:
found=str.find(str2);
if (found!=string::npos)
cout << "first 'needle' found at: " << int(found) << endl;

found=str.find("needles are small",found+1,6);
if (found!=string::npos)
cout << "second 'needle' found at: " << int(found) << endl;

found=str.find("haystack");
if (found!=string::npos)
cout << "'haystack' also found at: " << int(found) << endl;

found=str.find('.');
if (found!=string::npos)
cout << "Period found at: " << int(found) << endl;

// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
cout << str << endl;

return 0;
}


string的扩展

//stringutil.h
#include <string>
#include <vector>
class StringUtil {
public:
static std::string trim(std::string str);
static void split(std::string src, std::string delim, std::vector<std::string>& retVector);
};

 

//stringutil.cpp
#include "stringutil.h"
#include <cstring>
#include <iostream>
std::string StringUtil::trim(std::string str) {
using namespace std;
string dest = str;

string::iterator i;
for (i = dest.begin(); i != dest.end(); i++) {
if (!isspace(*i)) {
dest.erase(dest.begin(), i);
break;
}
}

if (i == dest.end()) {
return dest;
}

for (i = dest.end() - 1; i != dest.begin(); i--) {
if (!isspace(*i)) {
dest.erase(i + 1, dest.end());
break;
}
}

return dest;
}

void StringUtil::split(std::string src, std::string delim, std::vector<std::string>& retVector) {
using namespace std;
size_t last = 0;
size_t index = src.find(delim,last);
while (index != string::npos)
{
string sub = src.substr(last,index-last);
retVector.push_back(sub);
last = index+1;
index = src.find(delim,last);
}
if (last != (src.length()))
{
retVector.push_back(src.substr(last,index-last));
}
}

int main() {
using namespace std;
string str1 = "123\t";
string str2 = " 123 ";
cout << "str1 trim:[" << StringUtil::trim(str1) << "]" << endl;
cout << "str2 trim:[" << StringUtil::trim(str2) << "]" << endl;
string str3 = "a;b;c;d";
string delim = ";";
vector<string> resultVec;
StringUtil::split(str3, delim, resultVec);
for (int i = 0; i< resultVec.size(); i++) {
cout << "str " << resultVec[i] << endl;
}
return 0;
}


更多操作

    参考 http://www.cplusplus.com/reference/string/string/



 



posted on 2012-01-03 13:41  ggjucheng  阅读(8543)  评论(0编辑  收藏  举报