创建字符串

1.在使用string时候,用到的Index,需要注意的是,数组都是从0开始的,即index是从0开始的。

2.通常会使用的参数为(index,n)即为从index位置开始的第n个字符。

3.创建string类的方法:

string newString;// 法一
string message("hello");//法二,创建的同时进行赋值

char charArray[]= {'H','e','l','l','o','\0'};//需要注意的是,最后需要以"\0"结尾,这是用c语言的方式创建字符串
string message1(charArray);//即将其赋值到message1中

 常用操作方法

追加字符(append)

一系列的重载函数可以将新的内容附加到一个字符串中

string s1("Welcome");
s1.append(" to c++"); //添加" to c++"到s1
cout <<s1 <<endl;//Welcome to c++

string s2("Welcome");
s1.append(" to c and c++",3,2); //添加"c"到s2
cout <<s2<<endl;//Welcome c

string s3("Welcome");
s1.append(" to c and c++",5); //添加"to c"到s3,注意此处和搜索不同
cout <<s3 <<endl;//Welcome to c

string s4("Welcome");
s1.append(4,"G"); //添加"GGGG" 到s4
cout <<s4 <<endl;// WelcomeGGGG

替换字符(assign)方法和append同

一系列的重载函数可以将一个字符串赋值给新内容

string s1("Welcome");
s1.assign("Dallas"); //重新赋值"Dallas"到s1
cout <<s1 <<endl;//s1变成了Dallas

string s2("Welcome");
s1.assign("Dallas, Texas",1,3); //重新赋值"all"到s2
cout <<s2<<endl;//s2变成了all

string s3("Welcome");
s1.assign("Dallas, Texas",6); //重新赋值"Dallas"到s1
cout <<s3 <<endl;//s3变成了Dallas

string s4("Welcome");
s1.assign(4,"G"); //赋值"GGGG" 到s4
cout <<s4 <<endl;// GGGG

清除字符:

at(index):返回当前字符串中index位置的字符

clear():清空字符串

erase(index,n):删除字符串从index开始的第n个字符

empty():检测字符串是否为空

string s1("Welcome");
cout<<s1.at(3)<<endl;//返回c
cout<<s1.erase(2,3)<<endl;//返回Weme
s1.clear();//s1变成empty
cout<<s1.empty()<<endl;//返回1,即成功

比较字符串

compare()函数用于比较两个字符串,和C语言中的strcmp()函数很像;

string s1("Welcome");
string s2("Welcomg");
cout<<s1.compare(s2)<<endl;//返回-2
cout<<s2.compare(s1)<<endl;//返回2
cout<<s1.compare(Welcome)<<endl;//返回0

c++的做法相当于将把第一个减去第二个。

获取子串

at()函数用于获取一个独立的字符,substr()函数则用于获取一个子串

string s1("Welcome");
cout<<s1.substr(0,1)<<endl;//从0号位置开始的一个字符,返回W
cout<<s1.substr(3)<<endl;//从3号位置开始到末尾的字符,返回come
cout<<s1.substr(3,3)<<endl;//从3号位置开始的3个字符,返回com

搜索字符串

find()函数可以在一个字符串中搜索一个子串或者一个字符

string s1("Welcome to HRML");
cout<<s1.find("co")<<endl;//return 3 返回子串出现的第一个位置
cout<<s1.find("co",6)<<endl;//return -1 从6号位置开始,返回子串出现的第一个位置
cout<<s1.find("o")<<endl;//return 4 返回子串出现的第一个位置
cout<<s1.find("o",6)<<endl;//return 9 从6号位置开始,返回子串出现的第一个位置

插入和替换字符

insert():将某个字符/字符串插入当前字符串的某个位置

replace():将本字串从某个位置开始的一些字符替换成其他内容

string s1("Welcome to HTML");
s1.insert(11,"C++ and");
cout<<s1<<endl;//Welcome to C++ and HTML

string s2("AA");
s2.insert(1,4,"B");//1号位置连续插入4个相同的字符
cout<<s2<<endl;//ABBBBA

string s3("Welcome to HTML");
s3.replace(11,4,"C++");//从11号位置开始替换4个字符,注意'\0'(是C语言风格的字符串,所以有个\0)
cout<<s3<<endl;//Welcome to C++

字符串对象运算符

Operator

Description

[]

用数组下标运算符访问字符串中的字符

=

将一个字符串的内容复制到另一个字符串

+

连接两个字符串得到一个新串

+=

将一个字符串追加到另一个字符串末尾

<<

将一个字符串插入一个流

>>

从一个流中读取一个字符串,分界为空或者空结束符

==,!=,<,<=,>,>=

用于字符串的比较运算

例子

string s1 = "ABC";//赋值
string s2 = s1;//复制赋值
for (int i = s2.size()-1;i>=0;i--)
    cout <<s2[i];    //逐个输出ABC

string s3 = s1 + "DEFG";//ABCDEF
cout<<s3<<endl;//输出ABCDEF

s1 += "ABC";//和append相同
cout <<s1<<endl;// ABCABC

s1 = "ABC";
s2 = "ABE";
cout <<(s1 == s2)<<endl; //0
cout <<(s1 != s2)<<endl; //1
cout <<(s1 > s2)<<endl;  //0
cout <<(s1 >= s2)<<endl; //0
cout <<(s1 < s2)<<endl;  //1
cout <<(s1 <= s2)<<endl; //1