【ThinkingInC++】71、深入理解字符串

来源:互联网 发布:qt编程入门windows 编辑:程序博客网 时间:2024/05/17 02:37

第三章 深入理解字符串

我们希望string类能够做到的:

1.      创建或修改string中存放的字符序列

2.      检测string中元素的存在性

3.      能够在多种描述string字符的方案之间进行转换

3.2 创建并初始化C++字符串

SmallString.cpp

/*** 书本:【ThinkingInC++】* 功能:1、用C语言的char型数组或C++string类两者任一个的一部分*      2、用operator+来将不同的初始化数据源结合在一起*      3、用string对象的成员函数substr()来创建一个子串* 时间:2014年10月11日18:38:43* 作者:cutter_point*/#include <string>#include <iostream>using namespace std;int main(){    string s1("What is the sound of one clam napping?");    string s2("Anything worth doing is worth overdoing.");    string s3("I saw cutter_point in a UFO");    string s4(s1, 0, 8);    //从s1的0位开始,拷贝8个字符到s4    cout<<s4<<endl; //what is    string s5(s2, 15, 6);   //从15位开始,拷贝6个,不包括第15个字符,string是按从0开始计数的    cout<<s5<<endl; //如果按从0开始计数的话,那么就包含第15位就是包含s2[15]了    //doing    string s6(s3, 6, 15);   //把s3中从第6个字符开始一共15个字符拷贝到s6中    cout<<s6<<endl; //cutter_point in    //a.substr(c, d)截出a中从第c个字符开始,截取d个字符返回    string quoteMe=s4+"that"+s1.substr(20, 10)+s5+"with"+s3.substr(5, 100)+s1.substr(37, 1);    cout<<quoteMe<<endl;    return 0;}


 

3.3 对字符串进行操作

3.3.1 追加、插入和连接字符串

StrSize.cpp

/*** 书本:【ThinkingInC++】* 功能:追加、插入和连接字符串* 时间:2014年10月11日18:38:48* 作者:cutter_point*/#include <string>#include <iostream>using namespace std;int main(){    string s("I saw cutter_point in a UFO.");    cout<<s<<endl;  //输出这个字符串    //输出字符长度    cout<<"Size = "<<s.size()<<endl;    //输出系统给予S的内存空间大小    cout<<"内存大小(capacity) : "<<s.capacity()<<endl;    //给字符串添加一段字符    //string s.insert(int a, string b); 把字符串b插入到a的位置,就是把b插入到s[a]前面    s.insert(1, " thought I");    cout<<s<<endl;    //输出字符长度    cout<<"Size = "<<s.size()<<endl;    //输出系统给予S的内存空间大小, 这个时候是56,就是空间增加了一倍    cout<<"内存大小(capacity) : "<<s.capacity()<<endl;    //修改给予的内存的大小    s.reserve(500);    //在字符后面添加一串字符    s.append("I work very too hard, but I like C++.");    cout<<s<<endl;    //输出字符长度    cout<<"Size = "<<s.size()<<endl;    //输出系统给予S的内存空间大小, 这个时候是56,就是空间增加了一倍    cout<<"内存大小(capacity) : "<<s.capacity()<<endl;    return 0;}


 

 

3.3.2 替换字符串中的字符

StringReplace.cpp

 

/*** 书本:【ThinkingInC++】* 功能:替换字符串中的字符* 时间:2014年10月11日18:39:14* 作者:cutter_point*/#include <cassert>#include <string>#include <iostream>using namespace std;int main(){    string s("A piece of text");    string tag("$tag$");    s.insert(8, tag+' ');   //向第8个字符后面添加这个字符    cout<<s<<endl;    assert(s == "A piece $tag$ of text");    int start=s.find(tag);  //在s中找到tag的首个位置索引    cout<<start<<endl;    assert(start == 8);    assert(tag.size() == 5);    s.replace(start, tag.size(), "hello there");    //把s中第start个字符开始长度是tag.size()的替换成后面的hello there    assert(s == "A piece hello there of text");    cout<<s<<endl;    return 0;}


 

 

 

Replace.cpp

/*** 书本:【ThinkingInC++】* 功能:替换一段字符串* 时间:2014年10月11日18:39:45* 作者:cutter_point*/#include <iostream>#include <cassert>#include <cstddef>#include <string>using namespace std;//在modifyMe中吧findMe替换成newCharsvoid replaceChars(string& modifyMe, const string& findMe, const string& newChars){    size_t i=modifyMe.find(findMe, 0);  //从0开始找到的第一个findMe 的位置索引    if(i != string::npos)   //数据成员npos是string类的一个静态常量成员,他表示一个不存在的字符位置。    {        modifyMe.replace(i, findMe.size(), newChars);    }}int main(){    string s="I thought I saw cutter_point in a UFO."             "I have been working too hard, but I like C++ very much!";    string replacement("XF");    string findMe("UFO");    //调用函数    replaceChars(s, findMe, replacement);    cout<<s<<endl;    return 0;}


 

3.4 字符串的查找

Find() 在一个字符串中查找一个指定的单个字符或字符组。如果找到,就返回首次匹配的开始位置;如果没有查找到匹配的内容,则返回npos

 

Find_first_of() 在一个目标中进行查找,返回值是第一个与指定字符组中任何字符匹配的字符位置。如果没有查找到匹配的内容,则返回npos。

 

Find_last_of() 在一个目标串中进行查找,返回最后一个与指定字符组中任何字符匹配的字符位置。如果没有查找到匹配的内容,则返回npos

 

Find_first_not_of() 在一个目标串中进行查找,返回第一个与指定字符组中任何字符都不匹配的元素的位置。如果找不到那样的元素则返回npos。

 

Find_last_not_of() 在一个目标串中进行查找,返回下标值最大的与指定字符组中任何字符都不匹配的元素位置。若找不到那样的元素则返回npos

 

Rfind() 对一个串从尾至头查找一个指定的单个字符或字符组。如果找到,就返回首次匹配的开始位置。如果没有查找到匹配的内容,则返回npos.

3.4.3 从字符串中删除字符

用erase()成员函数删除字符串.

一个参数是要删除的起始位置,另外一个参数是要删除的个数。
0 0