#include"string_exercise.h"
// 1. 创建string的构造函数
void string_exercise_constructor()
{
cout << "---string的构造函数的方法---" << endl;
string s1;// 创建空字符串
cout << "str1: " << s1 << endl;
const char* str = "hello cpp";
string s2(str);//把c_string转换成string
cout << "str2: " << s2 << endl;
string s3(s2);// 调用拷贝构造函数
cout << "str3: " << s3 << endl;
string s4(10, 'a');// 创建10个a的字符串
cout << "str4: " << s4 << endl;
cout << endl;
}
// string赋值
void string_assignment()
{
cout << "---string赋值---" << endl;
// 用=号
string str1 = "hello cpp";
cout << "str1: " << str1 << endl;
string str2 = str1;// 赋值
cout << "str2: " << str2 << endl;
string str3;
str3 = 'a'; // 给单字符赋值,单必须要分开写
cout << "str3: " << str3 << endl;
// 用assign
string str4;
str4.assign("hello cpp!");
cout << "str4: " << str4 << endl;
string str5;
str5.assign("hello cpp!", 5);
cout << "str5: " << str5 << endl;
cout << "str5[1]: " << str5[1] << endl;
string str6;
str6.assign(str5);
cout << "str6: " << str6 << endl;
string str7;
str6.assign(5,'b');
cout << "str7: " << str7 << endl;
cout << endl;
}
// 拼接string
void string_splicing()
{
string s1 = "I";
s1 += " love China";
s1 += "!";
cout << "s1: " << s1 << endl;//s1: I love China!
string s2 = "But I don't like";
string s3 = s1 + s2;
cout << "s3: " << s3 << endl;//s3: I love China!But I don't like
s3.append(" Japan");
cout << "s3: " << s3 << endl;//s3: I love China!But I don't like Japan
s3.append(" UK and USA",3);// 前3位字符
cout << "s3: " << s3 << endl;//s3: I love China!But I don't like Japan UK
string s4 = "USA and France";
s3.append(s4, 3, s4.size());// 从下标3开始到字符末位
cout << "s3: " << s3 << endl;//s3: I love China!But I don't like Japan UK and France
s3.append(3, '!');// 3个!
cout << "s3: " << s3 << endl;//s3: I love China!But I don't like Japan UK and France!!!
cout << endl;
}
// 查找和替换
void string_find_replace()
{
cout << "--- 查找和替换 ---" << endl;
string s1 = "abcdefg";
int pos = s1.find("d");
if (pos == -1){cout << "Couldn't find it." << endl;}// 找不到会返回-1
else{cout << "Find it!" << endl;}
pos = s1.rfind("e");// 从右开始找
cout << "rfind pos: " << pos << endl;// 返回下标 4 下标是从左到右的下标
s1.replace(1, 3, "1111"); // 从下标1开始顺数3位数替换位 1111
cout << s1 << endl;// a1111efg
cout << endl;
}
// 字符串比较:
// 字符串对比主要是用于比较两个字符串是否相等
// 判断谁大谁小的意义并不是很大
void string_compare()
{
string s1 = "abcdefg";
string s2 = "abcdefg";
int result1 = s1.compare(s2);// 将每个字符ASCII码的值进行对比
cout << "result1: " << result1 << endl;
if (result1 == 0) {
cout << "s1 等于 s2" << endl;
}
else if (result1 > 0)
{
cout << "s1 大于 s2" << endl;
}
else
{
cout << "s1 小于 s2" << endl;
}
}
// 字符串存取
void string_set_get()
{
string s1 = "helle cpp";
// 遍历打印字符串
// 方法1 用[]
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
// 方法2 用at()
for (int i = 0; i < s1.size(); i++)
{
cout << s1.at(i) << " ";
}
cout << endl;
// 修改字符
s1[0] = 'x';
cout << "s1: " << s1 << endl;
s1.at(1) = 'v';
cout << "s1: " << s1 << endl;
cout << endl;
}
// 插入删除字符
void string_insert_del()
{
string s1 = "hello";
s1.insert(1, "xxx");;// 从下标1 插入xxx
cout << "s1: " << s1 << endl;
s1.erase(1, 3);// 删除从下标1 到下标3
cout << "s1: " << s1 << endl;
}
// 子串
void string_substr()
{
string s1 = "makchikin@qq.com";
int pos = s1.find("@");
string use_name = s1.substr(0, pos);// 从下标0到pos下标的字符串
cout << "usename: " << use_name << endl;// usename: makchikin
string s2 = "abcdefg";
string s3 = s2.substr(1, 3);// 获取某段字符
cout << "s3: " << s3 << endl;// s3: bcd
}
#include <iostream>
#include <string>
using namespace std;
void string_exercise_funtion() {
string http = "www.makchikin.com";
// 输出字符串的长度
cout << http.length() << endl;
cout << http.size() << endl;
// 拼接
http.append("/c++"); // 添加字符串
cout << http << endl;
cout << http.length() << endl; // 输出字符串的长度
// 查找起始位置
int pos = http.find("/c++"); // 查找/c++在字符串中的起始位置
cout << pos << endl;//www.makchikin.com/c++
// 替换
http.replace(pos, 7, "/python_"); // 从位置pos开始,之后的6个字符替换为python_
cout << http << endl;//www.makchikin.com/python_
// 删除
int pos2 = http.find("_");
http.replace(pos2, 1, "");//从位置pos2开始,之后的4个字符替换为空,即删除
cout << http << endl; //www.makchikin.com/python
// 找makchikin
int fisrt = http.find_first_of("."); // 从头开始寻找字符'.'的位置
cout << fisrt << endl;// 3
int last = http.find_last_of("."); // 从尾开始寻找字符'.'的位置
cout << last << endl;// 13
cout << http.substr(fisrt, last)<<endl; //.makchikin.co
cout << http.substr(4, 9) << endl; //makchikin 意思是从4的下标开始连续打印9位数下标的位置
cout << http.substr(fisrt+1, last-fisrt-1) << endl; //makchikin
// 字符串字面值与标准库string不是同一种类型
string s("hello");
cout << s.size() << endl; //OK
//cout << "hello".size() << endl; //ERROR
cout << s + "world" << endl;//OK
//cout << "hello" + "world" << endl;//ERROR
// strlen、sizeof与size()求字符串长度的区别
cout << strlen("123") << endl;// 获取字面长度
cout << size("123") << endl; // 获取
string ss = "123";
cout << ss.size() << endl;// 获取string类型长度
}