#include <iostream>

#include <string>
using namespace std;
int main()
{

string strText("This is a test"); //用const char * 构造strText对象
strText.append("!"); //在strText的末尾附加一个字符串"!"

char &ch=strText.at(2); //at()返回字符串在指定索引处的字符的引用
ch='T'; //如果给定的索引值超出了字符串的有效长度,则抛出out_of_range异常

const char *cstr=strText.c_str(); //将字符串strText以C风格的字符数组形式返回

const char *firstCh=strText.data(); //data()返回指向自己第一个位置的指针

cout<<strText.empty()<<endl; //判断字符串是否为空,即是字符串的长度是否为0

//basic_string &erase(size_type index=0,size_type num=npos)
//删除从index索引开始的num个字符,返回*this
//strText.erase(7,2).erase(5,3);

//返回字符'w'在字符串strText中第一次出现的位置,如果没找到,则返回string::npos
int loc=strText.find('w');
if(loc!=string::npos)
{
cout<<"find it!position:"<<loc<<endl;
}


else
{
cout<<"can't find it"<<endl;
}
//查找在字符串中第一个与str中某个字符匹配的字符,返回它的位置。
//搜索从index开始,如果没找到就返回string::npos
int loc2=strText.find_first_of("wwT",0,strText.length());


//substr()返回本字符串的一个字串,
string rs=strText.substr(4);

string str1="First string";
string str2("Second string");

//交换两个字符串str1和str2的内容
str1.swap(str2);

//length()返回字符串中字符的长度
//注意:字符串string对象并不是以'\0作为结束符,并且string对象根本就没有结束符'
cout<<strText.length()<<endl;
}
#include<iostream>
#include<string>
#include <cstring>

using namespace std;
int main()
{
//string构造函数
string a=string();
string b=string(15,'s');
const char *str="Oh,my good honey!";
string c=string(str);
string d=string(str,10);
string e=string(b,0,10);
string f=string(c.begin(),c.end());

//测试赋值结果
cout << "String a: " << a << endl; //
cout << "String b: " << b << endl; //sssssssssssssss
cout << "String c: " << c << endl; //Oh,my good honey!
cout << "String d: " << d << endl; //Oh,my good
cout << "String e: " << e << endl; //ssssssssss
cout << "String f: " << f << endl; //Oh,my good honey!

cout << endl;

//在字符串的末尾添加文本
a.append(c);
b.append(str);
c.append(str,10); //
d.append(b,2,11);
e.append(4,'q');
f.append(a.begin(),a.end());


//测试文本添加结果
cout << "String a: " << a << endl; //Oh,my good honey!
cout << "String b: " << b << endl; //sssssssssssssssOh,my good honey!
cout << "String c: " << c << endl; //Oh,my good honey!Oh,my good
cout << "String d: " << d << endl; //Oh,my goodsssssssssss
cout << "String e: " << e << endl; //ssssssssssqqqq
cout << "String f: " << f << endl; //Oh,my good honey!Oh,my good honey!

cout << endl;

//string赋值
a.assign(b);
b.assign(str);
c.assign(str,10);
d.assign(a,15,17);
e.assign(15,'z');

//测试赋值结果
cout << "String a: " << a << endl; //sssssssssssssssOh,my good honey!
cout << "String b: " << b << endl; //Oh,my good honey!
cout << "String c: " << c << endl; //Oh,my good
cout << "String d: " << d << endl; //Oh,my good honey!
cout << "String e: " << e << endl; //zzzzzzzzzzzzzzz

cout << endl;

//取字符串中指定位置的字符
cout << "The fourth character of String b: " << b.at(3) << endl; //The fourth character of String b: m

//测试字符串begin()和end()
string::iterator p_begin = b.begin();
string::iterator p_end = b.end();
cout << "b.begin() & b.end(): "; //b.begin() & b.end(): Oh,my good honey!
for(;p_begin < p_end;p_begin++)
cout << *p_begin;

cout << endl;

//测试c_str()
char *ce = (char *)b.c_str();
cout << "b.c_str(): " << ce << endl; //b.c_str(): Oh,my good honey!

//测试capacity
cout << "b.capacity():" << b.capacity() << " b.size():" << b.size() << endl; //b.capacity():32 b.size():17

//比较两个字符串
int fl = a.compare(b);
if(fl > 0)
cout << "String a is bigger than String b." << endl; //String a is bigger than String b.
else if(fl < 0)
cout << "String a is equal to String b." << endl;
else
cout << "String a is smaller than String b." << endl;

//将字符串复制到字符数组
char arr[20];
memset(arr,'\0',20);
b.copy(arr,20,0);
cout << "The character array is: " << arr << endl; //The character array is: Oh,my good honey!

//指向字符串第一个字符的指针
char *pstr=(char*)(b.data());
cout << "Point to the first character of String b: " << pstr << endl; // Point to the first character of String b: Oh,my good honey!

//判断字符串是否为空
cout<< "Is string b empty? Answer: " << boolalpha << b.empty() << endl; //Is string b empty? Answer: false

//删除指定的字符集
cout << "Delete the appointed character[2,4): " << b.erase(2,2) << endl; //Delete the appointed character[2,4): Ohy good honey!

//查找字符在字符串中首次出现的位置
cout << "The position of character \'g\' appear in String b: " << b.find('g',0) << endl; //The position of character 'g' appear in String b: 4

//查找字符串个任意一个字符在另一个字符串中出现的位置
cout << "The position of any character of \"abcd\" appear in String b: " << b.find_first_of("abcd",0) << endl; //The position of any character of "abcd" appear in String b: 7


//查找字符串个任意一个字符在另一个字符串都没有出现的位置
cout << "The position of any character of \"Oh,my\" appear in String b: " //The position of any character of "Oh,my" appear in String b: 3
<< b.find_first_not_of("Oh,my",0) << endl;

//字符串的长度,不包括字符串结束符,和b.size()的返回值一样
cout << "The length of String b is: " << b.length() << endl; //The length of String b is: 15

//字符串中最多能保存的字符数
cout << "The max_size the the String b can be holded: " << b.max_size() << endl; //The max_size the the String b can be holded: 9223372036854775807

//rbegin() & rend()
cout << "rbegin() & rend(): "; //rbegin() & rend(): !yenoh doog yhO
string::reverse_iterator r_begin = b.rbegin();
string::reverse_iterator r_end = b.rend();
for(;r_begin < r_end;r_begin++)
cout << *r_begin;
cout << endl;
//替换字符串中从一个位置到另一个位置的字符集
cout << "After replace[2,7),the result is: " << b.replace(2,5," bad") << endl; //After replace[2,7),the result is: Oh badd honey!

//设置字符串的保留空间,第一次的值为31,以后每次增加32
//即若reserve的参数大于31+32(n),小于等于31+32(n+1),则capacity的值为31+32(n+1)
cout << "Before reserve,the space is: " << b.capacity() << endl; //Before reserve,the space is: 32
b.reserve(35);
cout << "After reserve,the space is: " << b.capacity() << endl; //After reserve,the space is: 64

//改变字符的大小,若设置的大小n小于字符串的长度,则字符串变为由前n个字符组成
cout << "Before resize,the size is: " << b.size() << endl; //Before resize,the size is: 14
b.resize(10);
cout << "After resize,the size is: " << b.size() << " String b:" << b << endl; //After resize,the size is: 10 String b:Oh badd ho
//查找字符在字符串中最后一次出现的位置
cout << "The position that the character \'h\' last appear: " << b.rfind("h") << endl; //The position that the character 'h' last appear: 8

//求一个字符串的子字符串
cout << "The substring of String b(3,4):" << b.substr(3,4) << endl << endl; //The substring of String b(3,4):badd

//交换两个字符串的值
cout << "Before swap,the strings are:" << endl; //Before swap,the strings are:
cout << "String a:" << a << endl; //String a:sssssssssssssssOh,my good honey!
cout << "String b:" << b << endl; //String b:Oh badd ho
b.swap(a);
cout << "After swap,the strings are:" << endl;//After swap,the strings are:
cout << "String a:" << a << endl; //String a:Oh badd ho
cout << "String b:" << b << endl << endl; //String b:sssssssssssssssOh,my good honey!

return 0;
}
#include<string>
#include<iostream>
using namespace std;

int main()
{
//利用不同的构造函数进行初始化
string str1(5,'c'); //将连续的五个C赋值给字符串str1
string str2("This_is_the_str2"); //赋值给字符串str2
string str3(str2,4,7); //将字符串str2的第五位开始的最多7个字符赋值给str3
string str4("This_is_the_str4",6); //将str2的前6个字符赋值给str4
string str5(str1); //利用拷贝构造函数对str5进行赋值

cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
cout<<"str3:"<<str3<<endl;
cout<<"str4:"<<str4<<endl;
cout<<"str5:"<<str5<<endl<<endl;

/*执行结果
str1:ccccc
str2:This_is_the_str2
str3:_is_the
str4:This_i
str5:ccccc
*/

str1.assign(str2,4,4); //对str1进行重新赋值
cout<<"str1.assign():"<<str1<<endl<<endl;

str1.swap(str5); //将str1和str5的内容进行互换
cout<<"after swap: "<<"str1:"<<str1<<" "<<"str5:"<<str5<<endl<<endl;

str1.append("abcdef"); //在str1后面追加字符串
cout<<"str1.append(): "<<str1<<endl<<endl;

str1.insert(3,"xyz"); //从第四位开始插入字符串,其他字符后移
cout<<"str1.insert():"<<str1<<endl<<endl;

cout<<"str1.at(4):"<<str1.at(4)<<endl<<endl; //显示字符串str1[4]上的内容

str1.erase(4); //删除第五位及其以后的所有字符
cout<<"after erase(4),the str1:"<<str1<<endl<<endl;


/*执行结果
str1.assign():_is_
after swap: str1:ccccc str5:_is_
str1.append(): cccccabcdef
str1.insert():cccxyzccabcdef
str1.at(4):y
after erase(4),the str1:cccx
*/


//输出str2的长度,分别利用length()函数和size()函数
cout<<"str2.length: "<<str2.length()<<endl;
cout<<"str2.size: "<<str2.size()<<endl<<endl;

//输出str2所能存放的最大字符数
cout<<"str2.max_size: "<<str2.max_size()<<endl<<endl;

//从str[3]的位置开始替代
str4.replace(3,str2.length(),str2);
cout<<"str4.replace(3,str2.length(),str2): "<<str4<<endl<<endl;

/*执行结果
str2.length: 16
str2.size: 16
str2.max_size: 9223372036854775807
str4.replace(3,str2.length(),str2): ThiThis_is_the_str2
*/



//输出str4的子集,并在这之后输出str4与其子集进行比对
cout<<"str4.substr(): "<<str4.substr()<<endl;
cout<<"str4.substr(4): "<<str4.substr(4)<<endl;
cout<<"after substr,the str4 is: "<<str4<<endl<<endl;

/*执行结果
str4.substr(): ThiThis_is_the_str2
str4.substr(4): his_is_the_str2
after substr,the str4 is: ThiThis_is_the_str2
*/
}