C++中提供了专门的头文件(注意不是string.h,这个是C份个股字符串相关函数的头文件),来支持string类型,string类定义隐藏了字符串的数组性质,让我们可以像处理普通变量那样处理字符串,string对象和字符数组之间的主要区别是:可以将string对象声明为简单变量,而不是数组。
一、构造函数
1
)
string(const char *s)
:将
string
对象初始化为
s
指向的字符串
string str("Hello!");
2
)
string(size_type n,char c)
:创建一个包含
n
个元素的
string
对象,其中每个元素都被初始化为字符
c
string str(10,'a');
3
)
string(const string &str)
:将一个
string
对象初始化为
string
对象
str
(复制构造函数)
string str1("hello!");
string str2(str1);
4
)
string()
:创建一个默认的
string
对象,长度为
0
(默认构造函数)
string str; // 创建一个空的
string
对象
string类的设计允许程序自动处理的string的大小,因此,上述代码创建了一个长度为0的string对象。
二、常见操作
1.获取string对象的长度
string size()或string.length()
string str("Hello,World!");
int strLen1 = str.length();
int strLen2 = str.size();
2.复制string对象
在C语言中,使用strcpy,strncpy,函数来实现字符串的复制。在C++中则方便很多,可以直接将一个string对象赋值给另一个对象
string str1("Hello,World");
string str2;
str2=str1;
3.string对象的拼接和附加
1)使用“+”操作符拼接两个字符串
string str1("hello");
string str2("world");
string str3=str1+str2;
2)使用+=操作符在字符串后面附加内容
string str1(""hello);
string str2(""world\n);
str1+=str2;
3)string.append()函数
string str1=""hello"
str1.append("C string";
4)string.push_back()函数
string str("Hello");
str.push_back("abc");
4.string对象的比较
1)C语言中,使用strcmp,strncmp函数来进行字符串的比较,在C++中,由于将string对象声明为了简单变量,故而对字符串的比较操作十分简单,直接使用关系运算符(==,!=,<,<=,>,>=)
string str1("hello");
string str2("hello");
if (
str1 == str2
)
cout << "str1 = str2" << endl;
else if (
str1 < str2
)
cout << "str1 < str2" << endl;
else
cout << "str1 > str2" << endl;
2)使用类似strcmp的函数来进行string对象的比较,string类提供的是string.compare()方法
int compare(const string&str) const;
int compare(size_t pos
,
size_t len
,
const string&str
)
const;
int compare(size_t pos
,
size_t len
,
const string&str, size_t subpos
,
size_t sublen
)
const;
int compare(const char * s
)
const;
int compare(size_t pos
,
size_t len
,
const char * s
)
const;
int compare(size_t pos
,
size_t len
,
const char * s
,
size_t n
)
const;
compare 方法的返回值如下:
1)返回 0
,表示相等;
2)返回结果小于 0
,表示比较字符串中第一个不匹配的字符比源字符串小,或者所有字符都匹配但是比较字符串比源字符串短;
3)返回结果大于 0
,表示比较字符串中第一个不匹配的字符比源字符串大,或者所有字符都匹配但是比较字符串比源字符串长。
3)
string str1 ("aBcdef");
string str2 ("AbcdEf");
string str3 ("123456");
string str4 ("123dfg");
//下面是各种比较方法
int m=str1.compare (str2); //完整的A
和
B
的比较
int n=str1.compare(1,5,str2); //"Bcdef"和"AbcdEf"
比较
int p=str1.compare(1,5,str2,4,2); //"Bcdef"和"Ef"
比较
int q=str3.compare(0,3,str4,0,3); //"123"和"123"
比较
5.使用string。substr()函数来获取字串
定义:
string substr
(
size_t pos = 0
,
size_t len = npos
)
const;
其中,pos
是子字符串的起始位置(索引,第一个字符的索引为
0
),
len
是子串的长度。这个函数的功能是:
复制一个 string
对象中从
pos
处开始的
len
个字符到
string
对象
substr
中去,并返回
substr
。
6.访问string字符串的元素
可以像 C
语言中一样,将
string
对象当做一个数组,然后使用数组下标的方式来访问字符串中的元素;
也可以使用 string.at(index)
的方式来访问元素(索引号从
0
开始):
string str("Hello,World!");
cout << str[1] << endl; // 使用数组下标的方式访问
string
字符串的元素
cout << str.at(1) << endl; // 使用
at
索引访问
string
字符串的元素
7.string对象查找操作
find()查找
1)从字符串的
pos
位置开始(若不指定
pos
的值,则默认从索引
0
处开始),查找子字符串
str
。如果找到,则返回 该子字符串首次出现时其首字符的索引;否则,返回 string::npos
:
size_type find (const string& str, size_type pos = 0) const;
2)从字符串的
pos
位置开始(若不指定
pos
的值,则默认从索引
0
处开始),查找子字符串
s
。如果找到,则返回 该子字符串首次出现时其首字符的索引;否则,返回 string::npos
:
size_type find (const char *s, size_type pos = 0) const;
3)从字符串的
pos
位置开始(若不指定
pos
的值,则默认从索引
0
处开始),查找
s
的前
n
个字符组成的子字符串。 如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回 string::npos
:
size_type find (const char *s, size_type pos, size_type n);
4)从字符串的
pos
位置开始(若不指定
pos
的值,则默认从索引
0
处开始),查找字符
ch
。如果找到,则返回该 字符首次出现的位置;否则,返回 string::npos
:
size_type find (char ch, size_type pos = 0) const;
string.rfind()查找
string.rfind() 与
string.find()
方法类似,只是查找顺序不一样,
string.rfind()
是从指定位置
pos 默认为字符串末尾)开始向前查找,直到字符串的首部,并返回第一次查找到匹配项时匹
配项首字符的索引。换句话说,就是查找子字符串或字符最后一次出现的位置。
3.string.find_first_of()
string.find_first_of()
方法在字符串中从指定位置开始向后(默认为索引
0
处)查找参数中任何一个字符 首次出现的位置。
string str("catjldkjfellmlm");
int pos = str.find_first_of("gat");
if (pos == string::npos) {
printf("没有匹配到\n");
return 0;
}
else
cout << "
在索引
" << pos << "
处匹配到
" << endl;
程序输出结果是:在索引
1
处匹配到。
所查找的字符串
gat
中,第一次出现在字符串
str
中的字是
'a'
, 该字符在 str
中的索引是
1。
4 string.find_last_of()
string.find_last_of()
方法在字符串中查找参数中任何一个字符最后一次出现的位置(也就
是从指定位置开始往前查找,第一个出现的位置)。
5 string.find_first_not_of()
string.find_first_not_of()
方法在字符串中查找第一个不包含在参数中的字符。
6 string.find_last_not_of()
string.find_last_not_of()
方法在字符串中查找最后一个不包含在参数中的字符(从指定位
置开始往前查找,第一个不包含在参数中的字符)。
8.插入操作
使用
string.insert()
进行插入操作,函数原型如下:
string
&
insert
(
size_t pos
,
const string
&
str
)
; //
在位置
pos
处插入字符串
str
string
&
insert
(
size_t pos
,
const string
&
str
,
size_t subpos
,
size_t sublen
)
; //
在位置
pos
处插入字符
串
str
的从位置
subpos
处开始的
sublen
个字符
string
&
insert
(
size_t pos
,
const char * s
)
; //
在位置
pos
处插入字符串
s
string
&
insert
(
size_t pos
,
const char * s
,
size_t n
)
; //
在位置
pos
处插入字符串
s
的前
n
个字符
string
&
insert
(
size_t pos
,
size_t n
,
char c
)
; //
在位置
pos
处插入
n
个字符
c
iterator insert (const_iterator p, size_t n, char c); //
在
p
处插入
n
个字符
c
,并返回插入后迭代器的位置
iterator insert (const_iterator p, char c); //
在
p
处插入字符
c
,并返回插入后迭代器的位置
9.删除操作
使用
string.erase()
进行元素删除操作
函数原型如下:
string& erase (size_t pos = 0, size_t len = npos); //
删除从
pos
处开始的
n
个字符
iterator erase (const_iterator p); //
删除
p
处的一个字符,并返回删除后迭代器的位置
iterator erase (const_iterator first, const_iterator last); //
删除从
first
到
last
之间的字符,并返回删除后迭 代器的位置
十、判断字符串是否为空
使用
empty()
函数判断字符串是否为空
string str;
if(str.
empty()
){
cout << "字符串为空" << endl;
}
十一、使用
swap
函数交换两个字符串
string str1 = "abc";
string str2 = "hjkui";
str1.swap(str2);
cout << str1 << endl;
cout
十二、输入输出
string str1,str2;
//
读入方式
1
遇到焕行停止
getline(cin, str1);
//
读入方式
2
遇到空格停止
cin>>str2;
//
输出方式
1
printf("%s\n" ,str1.c_ str());
//
输出方式
2
cout<<str2<<end1;
十三、字符串替换
string &replace(int p0, int n0,const char *s);
//
删除从
p0
开始的
n0
个字符,然后在
p0
处插入串
s
string &replace(int p0, int n0,const char *s, int n);
//
删除
p0
开始的
n0
个字符,然后在
p0
处插入字符串
s
的前
n
个字符
string &replace(int p0, int n0,const string &s);
//
删除从
p0
开始的
n0
个字符,然后在
p0
处插入串
s
string &replace(int p0, int n0,const string &s, int pos, int n);
//
删除
p0
开始的
n0
个字符,然后在
p0
处插入串
s
中从
pos
开始的
n
个字符
string &replace(int p0, int n0,int n, char c);
//
删除
p0
开始的
n0
个字符,然后在
p0
处插入
n
个字符
c
十四、字符串反转
使用
algorithm
头文件里面的
reverse
函数
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s1;
cin >> s1;
reverse(s1.begin(), s1.end());
cout << s1 << endl;
return 0;
}