String 的详细用法 在代码中分类
#include<iostream>
using namespace std;
#include<string>
// 本质上 string 是一个字符串类 内部是char* 数据,此外也包含成员函数
// 一. string 的构造函数
//1. string(); //构建一个空字符串 如 string str();此字符串为空字符
//2.string(char *);//用C类型字符串来初始化
//3.string(n,char a);//用n个字符型a来初始化
//4.string(string & str);//拷贝构造函数
void test0601()
{
//1. string();
string a;
//2.string(char *);
const char* B;
B = "hello hui !";
string b(B) ;
//3.string(n,char a);
string c(6, 'h');
//4.string(string & str);//拷贝构造函数
string d(c);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
cout << "d=" << d << endl;
}
//二.string 中赋值操作
/*
* 重载 = 运算符
1. string & operator=(char *); //用 char* 类型赋值
2.string & operator=(string & );//用 string 类型赋值
3.string & operator=(char );//用 char 类型赋值
* 利用内部函数 assign
4.string & assign(char *);//调用 char* 赋值
5.string & assign(string &);//调用 string 赋值
6.string & assign(n,char );//赋值为n个 char 字符
*/
void test0602()
{
//重载 = 运算符
//1. string & operator=(char*); //用 char* 类型赋值
string aa("hahahaha !");
cout << "aa=" << aa << endl;
const char* AA = "Hui";
aa = AA;
cout << "aa=" << aa << endl;
//2.string & operator=(string&);//用 string 类型赋值
string BB = "hui huan zai ma ?";
aa = BB;
cout << "aa=" << aa << endl;
//3.string & operator=(char );//用 char 类型赋值
char CC = 'H';
aa = CC;
cout << "aa=" << aa << endl;
//*利用内部函数 assign
//4.string & assign(char*);//调用char*
aa.assign("Hui hui ?");
cout << "aa=" << aa << endl;
//5.string & assign(string&);//调用string
string DD("Yi zhi zai fou ?");
aa.assign(DD);
cout << "aa=" << aa << endl;
//6.string & assign(char);//调用 char
aa.assign(6,'Z');
cout << "aa=" << aa << endl;
}
//三.拼接
/*
* 重载 += 运算符
* 1.string & operator+=(string & );
* 2.string & operator+=(char * );
* 3.string & operator+=( cahr );
* 利用append函数
* 4.string & append(string &);
* 5.string & append(char * );
* 6.string & append(char * ,int n);
* 7.string & append(char *,int post ,int n);
*/
void test0603()
{
//重载 += 运算符
//*1.string & operator+=(string&);
string A("Ha Ha ");
string B("Huan Xing Ma ");
//或者 A+="XXX XX XX";
A += B;
cout <<A<< endl;
//3.string & operator+=( cahr );
A += '?';
cout << A << endl;
//2.string & operator+=(char * );
const char* C = " Wei Ke Zhi Ye";
A += C;
cout << A << endl;
// 利用append函数
//*4.string & append(string&);
string D(" 用 append 函数");
A.append(D);
cout << A << endl;
//5.string & append(char * );
const char* E = "\n 且看:";
A.append(E);
cout << A << endl;
// 6.string & append(char * ,int n);//从第一个开始,选n个
const char* F = "123456";
//A.append(F, 2);
//cout << A << endl;
//*7.string & append(char*, int post, int n);//从 post位置开始 ,截取n个
A.append(F, 2, 2);
cout << A << endl;
}
//四.string查找与替换
/* find();
* 功能描述:
* 查找:查找指定字符串是否存在
* 替换:在指定的位置替换字符串
*
* 1.int find(const string& str, int pos = 0) const;//查找str第一次出现位置,从pos开始查找
* 2.int find(const char* s,int pos =0) const;//查找s第一次出现位置,从pos开始查找
* 3.int find(const char* s,int pos, int n) const;//从pos位置查找s的前n个字符第一次位置
* 4.int find(const char c,int pos =0)const;//查找字符c第一次出现位置
*
* rfind 从后往前看
* string & replace(int pos,int n,const string& str);//替换从pos开始n个字符为字符串str
* string & replace(int pos,int n,const char* s); //替换从pos开始的n个字符为字符串s
*/
void test0604()
{
string A("1234566789");
int pos=A.find('6');//找到 返回从前往后第一个位置
int rpos = A.rfind('6');//从后往前第一个位置
if (pos == -1)
{
cout << "未查到返回-1!!!" << endl;
}
else cout << "pos=" << pos << endl;
if (rpos == -1)
{
cout << "未查到!!!" << endl;
}
else cout << "rpos=" << rpos << endl;
A.replace(2, 2, "GGB");//实际上 第二个控制字符个数没起到作用,结果还是看给了多长字符
A.replace(5, 2,"G");
cout << A << endl;
}
//五.比较
/* int compare(string & str); 不过实际上只要比较是否相同就差不多了,一般比较大小没多大意义
* int compare(char * str );
* 利用compare函数 从前到后依次比较字母对应的Ascall值
* 相同返回 0 ,大于 1,小于 -1
*
*/
void test0605()
{
string A("hello");
string B("hello");
const char* C = "Hello";
string D("ijklm");
int com = A.compare(B);
cout << "com=" << com << endl;
com = A.compare(C);
cout << "com=" << com << endl;
com = A.compare(D);
cout << "com=" << com << endl;
}
//六.string类型存取
/* 取
* 1. char & operator[](int n);//利用[]取 不能检测数组下标是否越界,
* 2. char & at(int n);//利用at函数取 可以检测数组下标越界, 不过他们都会终止进程
* *****************************************************************************************
* *先立存 后修改
* 1. 也用上面两种方式
*/
void test0606()
{
string A("Hui Ge GG-Bone");
for (int i = 0; i < A.size(); i++)
{
cout << A[i] ;
}
cout << endl;
//for (int i = 0; i < A.size()+999; i++) //下标越界
for (int i = 0; i < A.size() ; i++)
{
cout << A.at(i);
}
cout << endl;
A[0] = 'h';
A.at(2) = '!';
cout << A;
}
//7.插入
/* string & insert(int pos,const string str);
string & insert(int pos,const char * str);
string & insert(int pos,int n,char c);//从pos位置插入n个c
*/
void test0607()
{
string A("GBone");
//A.insert(1, "G-"); //从 1开始,插入“G-”
cout << A.insert(1, "G-") << endl;
cout << A << endl;
cout << A.insert(7, 6, '6') << endl;
}
//8.提取子字符串
/*
* *利用 string substr(int pos,int n);//从pos开始的n个字符串
*/
void test0608()
{
string A("GG-Bone@qq.com");
//A.find('@');
string B = A.substr(0, A.find('@'));
cout << "Name:" << B;
}
int main() {
//test0601();
//test0602();
//test0603();
//test0604();
//test0605();
//test0606();
test0607();
//test0608();
}