String是C++字符串,本质上是一个类,类内部封装了char*,因此它是一个char*型的容器,其内部封装了很多成员方法,例如find,copy,delete,insert等。String管理char*所分配的内存,不用担心复制越界或者取值越界,由类的内部进行负责。
String(); //创建一个空的字符串
String(const char* s); //使用字符串s进行初始化
String(const string& str); //使用一个string对象初始化另一个string对象
String(int n, char c); //使用n个字符c初始化
一般来说,一般使用“=”的方式进行赋值。
- //第一种等号方式赋值
- string str1;
- str1 = "Hello world!";
- cout << "str1=" << str1 << endl;
- //第二种等号方式赋值
- string str2;
- str2 = str1;
- cout << "str2=" << str2 << endl;
- //第一种assign函数实现
- string str4;
- str4.assign("Hello C++!");
- cout << "str4=" << str4<< endl;
- //第二种assign函数实现,实现只赋值前几个字符
- string str5;
- str5.assign("Hello C++!",5);
- cout << "str5=" << str5<< endl;
- //第三种assign函数,将一个字符串赋值给另一个字符串
- string str6;
- str6.assign(str5);
- cout << "str6=" << str6 << endl;
功能:实现在字符串的末尾拼接字符串。
- //使用运算符+=
- string str1 = "我";
- str1 += "爱玩游戏";
- str1 += ':';
- cout << "str1=" << str1 << endl;
- string str2 = "LOL DNF";
- str1 += str2;
- cout << "str1=" << str1 << endl;
- //使用append函数
- string str3 = "I";
- str3.append(" love ");
- cout << "str3=" << str3 << endl;
- //append只拼接n个字符
- //str3.append("game abcde", 4); //拼接前四个字符
- str3.append("game abcde", 0,4); //拼接前四个字符,从第0个位置开始,截取4个字符
- cout << "str3=" << str3 << endl;
查找:查找指定字符串是否存在;
替换:在指定位置替换字符串;
- #include<iostream>
- #include"string"
- using namespace std;
- //1、查找
- void test01()
- {
- string str1 = "abcdefgde";
- int pos1=str1.find("de");
- cout << "pos="<<pos1 << endl; //输出3,因为索引从0开始
- int pos2 = str1.find("df");
- cout << "pos=" << pos2 << endl; //找不到则返回-1
-
- //find和rfind的区别
- //rfind从右往左查找,find从左往右查找
- int pos3 = str1.rfind("de");
- cout << "pos=" << pos3 << endl; //输出7
- }
- //2、替换
- void test02()
- {
- string str2 = "abcdefg";
- str2.replace(1, 3, "1111"); //一共替换了4个1进去,变成a1111efg,说明bcd替换成了1111
- cout << "str2=" << str2 << endl;
- }
- int main()
- {
- //test01();
- test02();
- }
功能:比较字符串的ASCII码,逐个挨个进行对比,如果两者相等则返0,如果前者大返回1,如果后者大返回-1。
Compare最大的用途就是比两者是否相等。但对比谁大谁小意义不大。
- #include<iostream>
- #include"string"
- using namespace std;
- void test01()
- {
- string str1 = "xello";
- string str2 = "yello";
- if (str1.compare(str2) == 0)
- {
- cout << "两者相等" << endl;
- }
- else if (str1.compare(str2) > 0)
- {
- cout << "str1>str2" << endl;
- }
- else
- {
- cout << "str1<str2" << endl;
- }
- }
- int main()
- {
- test01();
- }
作用:提取字符串中的具体字符。通常可以利用字符数组的下标来提取。也可以利用函数。
- #include<iostream>
- #include"string"
- using namespace std;
- void test01()
- {
- string str1 = "hello";
- for (int i = 0; i < str1.size(); i++)
- {
- cout << str1[i] << " ";
- }
- cout << endl;
- //修改字符
- str1[0] = 'H';
- cout << str1 << endl;
- }
- int main()
- {
- test01();
- }
String字符串通过Insert函数完成插入操作,通过erase删除指定位置的字符。
需要注意的是,下标都是从0开始算的。
- #include<iostream>
- #include"string"
- using namespace std;
- void test01()
- {
- string str = "Hello!";
- //插入
- str.insert(1, "111");//从第一个位置插入字符串
- cout << "str=" << str << endl; //输出H111ello
- //删除
- str.erase(1, 3);//从第1个位置起,删除3个字符
- cout << "str=" << str << endl; //输出Hello
- }
- int main()
- {
- test01();
- }
从一段字符串中截取一段我们想要的字符串。需要明确开始截断的位置和持续的字符数。
- #include<iostream>
- #include"string"
- using namespace std;
- void test01()
- {
- string str = "Hello!";
- string subStr = str.substr(1, 3);
- cout << "subStr=" << subStr << endl;
- }
- void test02()
- {
- string email = "zhangsan@sina.com";
- //从邮件地址中获取用户名信息
- //先找@的位置
- int pos = email.find("@"); //位置在8号
- string name = email.substr(0, pos); //从0开始,截8个位置就可以了
- cout << "name is " << name << endl;
- }
- int main()
- {
- //test01();
- test02();
- }