字符串操作的全面总结

电子说

1.2w人已加入

描述

字符串操作看似简单,其实非常重要,不注意的话,经常出现代码运行结果和自己想要的不一致,甚至崩溃。本文总结了一些构建string对象方法、修改string对象的方法、string类型的操作函数、string类型的查找、string对象的比较。

1 构建string对象方法

首先,为了在我们的程序中使用string类型,我们必须包含头文件 。如下:

#include

声明一个字符串变量很简单:

string Str;

这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str初始化为一个空字符串。

String类的构造函数和析构函数如下:

字符串

代码实例:

#include #include using namespace std; //20200527 测试字符串操作 C语言与CPP编程 int main() { string s1; cout <

** 运行结果**:字符串

2 修改string对象的方法

与容器共有的 string 操作:

字符串

代码实例:

#include #include using namespace std; //2020.05.27 测试字符串操作 公众号:C语言与CPP编程 int main() { string s("hello"); string s2("abcdef"); string::iterator p = s.begin(); //迭代器p s.insert(p,'A'); //在迭代器p指向的s开始之前插入A cout << s << endl; //s为Ahello s.insert(p,3,'B'); //p指向返回的Ahello的A处,在A之前插入3个B cout << s << endl; //s为BBBAhello string::iterator b = s2.begin(); //迭代器b string::iterator e = s2.end(); //迭代器e //p = s.begin(); //p指向s s.insert(p,b,e); //在p指向的s之前插入b和e迭代器范围内的元素abcdef cout << s << endl; //s为abcdefBBBAhello s = "hello"; cout << s << endl; //s为hello s.assign(b,e); //s所有的元素倍替换为b到e之间的元素,b与e之间为s2 cout << s << endl; //s为abcdef s.assign(8,'K'); cout << s << endl; //s为KKKKKKKK p = s2.begin(); //迭代器p指向s2的a s2.erase(p); //删除迭代器p指向的元素a cout << s2 << endl; //s2为bcdef p = s2.begin(); //a被删除,p指向b p++; //指向c p++; //指向d string::iterator p2 = s2.end(); //p2迭代器指向f p2--; //指向e s2.erase(p,p2); //删除p指向的d和p2指向的e之间的元素 cout << s2 << endl; //s2为bcf return 0; }

运行结果:

字符串运行结果

string 类型特有的版本:

string以数组的形式存储,可以用数组的下标进行修改操作:

字符串

代码实例:

#include #include using namespace std; //2020.05。27 测试字符串操作 公众号:C语言与CPP编程 int main() { string s("hello"); string s2("abc"); s.insert(0,3,'A'); //在s下标是0之前插入3个A cout << s << endl; //s为AAAhello s.insert(5,s2); //在AAAhello下标是5的元素之前插入abc cout << s << endl; //s为AAAheabcllo s2 = "123456"; s.insert(0,s2,2,3); //在s的下标是0之前插入s2下标为2开始往后的3个元素345 cout << s << endl; //s为345AAAheabcllo char *cp = "Stately plup Buck"; s.assign (cp,7); cout << s << endl; //s为Stately s.assign(cp); //没有长度,默认是拷贝全部 cout << s << endl; //s为Stately plup Buck s = "hello"; s.insert (0,cp,7); cout << s <

运行结果:

字符串

运行结果

3 适合string类型操作的函数

substr()主要功能是复制子字符串,要求从指定位置开始,并具有指定的长度。

append() 方法在被选元素的结尾(仍然在内部)插入指定内容。提示:如需在被选元素的开头插入内容,请使用prepend()方法。

replace() 该函数返回一个字符串,其中指定的字符串已经被替换为另一字符串,并且替换的次数也可以指定。

代码实例:

#include #include using namespace std; //2020.05.27 测试字符串操作 公众号:C语言与CPP编程 int main() { string s("Hello world"); string s2 = s.substr(6,5); //从第6个开始取5个 cout << s2 << endl ; //s2为world s2 = s.substr(6); //从第6个开始取拷贝所有的 cout << s2 << endl ; //s2为world s2 = s.substr(6); //s2拷贝s的全部,相当于s2=s cout << s2 << endl ; //s2为Hello world s = "C++ Primer"; s.append(" 3rd Ed"); //再s最后添加3rd Ed cout << s<< endl ; //s为C++ Primer 3rd Ed s = "C++ Primer"; s.insert(s.size()," 3rd Ed"); //最后插入 cout << s<< endl ; //s为C++ Primer 3rd Ed s.replace(11,3,"4th"); //下标11开始3个替换4th cout << s<< endl ; //s为C++ Primer 4th Ed s.replace(11,3,"Fourth"); //下标11开始3个替换Fourth cout << s<< endl ; //s为C++ Primer Fourth Ed s = "C++ Primer 3rd Ed"; //replace相当于先删除后插入 s.erase (11,3); //删除3rd s.insert(11,"Fourth"); //插入Fourth cout << s<< endl ; //s为C++ Primer Fourth Ed return 0; }

运行结果:

字符串

运行结果

4 string类型的查找

字符串

代码实例:

#include #include using namespace std; //2020.05.27 测试字符串操作 公众号:C语言与CPP编程 int main() { string name("AnnaBelle"); string::size_type pos1 = name.find("Bell"); cout << pos1 << endl; //返回下标4,如果没找到返回npos if(pos1 == string::npos) cout << "没找到!" << endl; else cout << "找到了!下标:" << pos1 <

运行结果:

字符串

运行结果

5 string对象的比较

字符串

代码实例:

#include #include #include using std::cout; using std::endl; using std::cin; using std::string; int main(void) { string str1="hi,test,hello"; string str2="hi,test"; //字符串比较 if(str1.compare(str2)>0) printf("str1>str2 "); else if(str1.compare(str2)<0) printf("str1

运行结果:

字符串

运行结果

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分