String的概念
string是STL的字符串类,通常用来表示字符串,而在使用string之前,字符串通常使用char 来表示的,string 与 char都可以表示字符串
*string和char 的比较:
1)string是一个类,char *是一个指向字符的指针
2)string中封装了char *,管理这个字符串,是一个char *类型的容器
3)string不用考虑内存释放和越界。
4)string管理char *申请的空间。每一次string的复制,取值都由string类负责维护,不用担心越界的问题。
5)string提供了一系列的字符串操作函数
查照 find 拷贝 copy 删除 erase 替换 replace 插入 insert
string 的构造函数
默认构造函数:
string(); //构造一个空的字符串 string s1。
拷贝构造函数:
string(const string &str);//构造一个与 str 一样的 string。如 string s1(s2)。
带参数的构造函数
string(const char *s); //用字符串 s 初始化
string(int n,char c); //用 n 个字符 c 初始化
string 的存取字符操作
string 类的字符操作:
const char &operator[] (int n) const;
const char &at(int n) const;
char &operator[] (int n);
char &at(int n);
operator[]和 at()均返回当前字符串中第 n 个字符,但二者是有区别的。
主要区别在于 at()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越
界时,编译器直接出错。如果你的程序希望可以通过 try,catch 捕获异常,建议采用 at()。
从 string 取得 const char*的操作
const char *c_str() const; //返回一个以’\0’结尾的字符串的首地址
把 string 拷贝到 char*指向的内存空间的操作
int copy(char *s, int n, int pos=0) const;
把当前串中以 pos 开始的 n 个字符拷贝到以 s 为起始位置的字符数组中,返回实际拷贝的数
目。注意要保证 s 所指向的空间足够大以容纳当前字符串,不然会越界
string 的长度
int length() const; //返回当前字符串的长度。长度不包括字符串结尾的’\0’。
bool empty() const; //当前字符串是否为空
string 的赋值
string &operator=(const string &s);//把字符串 s 赋给当前的字符串
string &assign(const char *s); //把字符串 s 赋给当前的字符串
string &assign(const char *s, int n); //把字符串 s 的前 n 个字符赋给当前的字符串
string &assign(const string &s); //把字符串 s 赋给当前字符串
string &assign(int n,char c); //用 n 个字符 c 赋给当前字符串
string &assign(const string &s,int start, int n); //把字符串 s 中从 start 开始的 n 个字符赋给当
前字符串
string 字符串连接
string &operator+=(const string &s); //把字符串 s 连接到当前字符串结尾
string &operator+=(const char *s);//把字符串 s 连接到当前字符串结尾
string &append(const char *s); //把字符串 s 连接到当前字符串结尾
string &append(const char *s,int n); //把字符串 s 的前 n 个字符连接到当前字符串结尾
string &append(const string &s); //同 operator+=()
string &append(const string &s,int pos, int n);//把字符串 s 中从 pos 开始的 n 个字符连接到当
前字符串结尾
string &append(int n, char c); //在当前字符串结尾添加 n 个字符 c
string 的比较
int compare(const string &s) const; //与字符串 s 比较
int compare(const char *s) const; //与字符串 s 比较
compare 函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典
顺序,排越前面的越小。大写的 A 比小写的 a 小。
string 的子串
string substr(int pos=0, int n=npos) const; //返回由 pos 开始的 n 个字符组成的子字符串
string 的查找 和 替换
查找
int find(char c,int pos=0) const; //从 pos 开始查找字符 c 在当前字符串的位置
int find(const char *s, int pos=0) const; //从 pos 开始查找字符串 s 在当前字符串的位置
int find(const string &s, int pos=0) const; //从 pos 开始查找字符串 s 在当前字符串中的位置
find 函数如果查找不到,就返回-1
int rfind(char c, int pos=npos) const; //从 pos 开始从后向前查找字符 c 在当前字符串中的位
置
int rfind(const char *s, int pos=npos) const;
int rfind(const string &s, int pos=npos) const;
//rfind 是反向查找的意思,如果查找不到, 返回-1
替换
string &replace(int pos, int n, const char *s);//删除从 pos 开始的 n 个字符,然后在 pos 处插入
串 s
string &replace(int pos, int n, const string &s); //删除从 pos 开始的 n 个字符,然后在 pos 处
插入串 s
void swap(string &s2); //交换当前字符串与 s2 的值
String 的区间删除和插入
string &insert(int pos, const char *s);
string &insert(int pos, const string &s);
//前两个函数在 pos 位置插入字符串 s
string &insert(int pos, int n, char c); //在 pos 位置 插入 n 个字符 c
string &erase(int pos=0, int n=npos); //删除 pos 开始的 n 个字符,返回修改后的字符串
#include <iostream>
using namespace std;
#include "string"
//#define D_SCL_SECURE_NO_WARNINGS
//String类的初始化
void main21()
{
string s1 = "yangyun";
string s2("lijiatu");
string s3 = s2; //拷贝构造函数
string s4(4, 'a');
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
cout << "s4: " << s4 << endl;
}
//string的遍历
void main22()
{
int i = 0;
string s1 = "yangyun";
//1 数组方式
for (i = 0; i < s1.length(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
//2 迭代器
for (string::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//3 at
try
{
for (i = 0; i < s1.length() + 3; i++)
{
cout << s1.at(i) << " "; //at会抛处异常
}
}
catch (...)
{
cout << "发生异常\n";
}
/*cout << "at之后\n";
//对比数组方式
try
{
for (i = 0; i < s1.length() +3 ; i++)
{
cout << s1[i] << " "; //出现错误不抛异常 程序down
}
}
catch (...)
{
cout << "发生异常\n";
}
*/
cout << endl;
}
//字符指针和string的转换
void main23()
{
//1 char * ==> string
string s1 = "yangyun";
//2 s1 ==> char * 显示出内存首地址
printf("s1: %s\n", s1.c_str() );
//3 s1中的内容拷贝到 buf中
//char buf1[128] = {0};
//s1.copy(buf1, 4, 0); //从零号位置开始拷贝4个字符,没有加\0
//cout << "buf1 :" << buf1 << endl;
}
//字符串连接
void main24()
{
string s1 = "yangyun";
string s2 = "lijiatu";
//1
s1 = s1 + s2;
cout << s1 << endl;
//2
string s3 = "zhan";
string s4 = "xiaoping";
s3.append(s4);
cout << s3 << endl;
}
//字符串的查找和替换
void mian25()
{
string s1 = "yangyun lijiatu aaa ccc aaa aaa";
string s2 = "lijiatu";
//1 第一次出现aaa的下标
int index = s1.find("aaa", 0); //从0下标开始检索
cout << "index : " << index << endl;
//2 求aaa 每一次出现的下标
int offindex = s1.find("aaa", 0);
while (offindex != string::npos) //offindex != -1
{
cout << "offindex : " << offindex << endl;
offindex = offindex + 1;
offindex = s1.find("aaa", offindex);
}
//3 aaa替换AAA
string s3 = "aaa bbb ccc";
s3.replace(0, 3, "AAA");
cout << s3 << endl;
offindex = s1.find("aaa", 0);
while (offindex != string::npos) //offindex != -1
{
cout << "offindex : " << offindex << endl;
s1.replace(offindex, 3, "AAA"); //从offindex开始删3个用AAA补充
offindex = offindex + 1;
offindex = s1.find("aaa", offindex);
}
cout << s1 << endl;
}
void main26()
{
string s1 = "hello1 hello2 hello3";
//删除
string::iterator it = find(s1.begin(), s1.end(), 'l');
if (it != s1.end())
{
s1.erase(it);
}
cout << "删除l之后s1: " << s1 << endl;
cout << "s1的长度 " << s1.length() << endl;
s1.erase(s1.begin(), s1.end());
cout << "全部删除s1 " << s1 << endl;
cout << "s1的长度 " << s1.length() << endl;
//插入
string s2 = "BBB";
s2.insert(0, "AAA");
cout << "s2 " << s2 << endl;
}
//string 算法相关
void mian27()
{
string s1 = "AAAbbb";
transform(s1.begin(), s1.end(), s1.begin(), toupper); //全部大写
cout << "s1 " << s1 << endl;
string s2 = "AAAbbb";
transform(s2.begin(), s2.end(), s2.begin(), tolower);//全部小写
cout << "s2 " << s2 << endl;
}
void main()
{
//main21();
//main22();
//main23();
//main24();
//mian25();
//main26();
mian27();
system("pause");
}