目录
7.string字符存取[].at,取,修改单个字符,size返回字符串长度
1.string基本概念
2.string构造函数,char*
#include<iostream>
using namespace std;
//string构造函数
void test01()
{
string s1;//默认构造,无参构造,string(),创建一个空字符串
string s = "abcd";
cout <<"s= "<< s << endl;
const char* str = "hello world";
//C语言风格的字符串初始化c++中的string
string s2(str);//string(const char *)
cout << "s2= " << s2 << endl;
string s3(s2);//拷贝构造,用string对象初始化另一个string对象
cout << "s3= " << s3 << endl;
string s4(10, 'a');//用10个a组成一个字符串
cout << "s4= " << s4 << endl;
}
int main()
{
test01();
/*
char* p = "abcef";//是将字符串a的地址给了p
*p = 'W';
"abcdf" 字符串是常量,常量不可以更改
所以报错
*/
cout << "__________________________" << endl;
const char* p = "abcef";
//常量指针,内容不可更改,指向可以更改
p = "fg";
cout << p << endl;
//为什么不是*p,是将字符串a的地址给了p,然后一直往后读
//读到\0为止,输出*p会输出a
system("pause");//按任意键继续
return 0;
}
s[0]是一个字符,而不是字符串
3.string赋值操作
#include<iostream>
using namespace std;
//string赋值操作
void test01()
{
string str1;
//char*类型字符串 赋值给当前字符串
str1 = "hello world";//用等号的方法做一个赋值操作
cout << "str1= " << str1 << endl;
string str11="zyq";
//char*类型字符串 赋值给当前字符串
cout << "str11= " << str11 << endl;
string str2;
//把字符串str1赋值给当前的字符串
str2 = str1;
cout << "str2= " << str2 << endl;
string str3;
//单个字符给字符串赋值,单个字符也可以看成字符串
str3 = 'a';
cout << "str3= " << str3 << endl;
/*
报错
string str33='a';
//单个字符给字符串赋值,单个字符也可以看成字符串
cout << "str33= " << str33 << endl;
*/
string str4;
//char*类型字符串 赋值给当前字符串
str4.assign("hello C++");
cout << "str4= " << str4 << endl;
string str5;
//把字符串s的前n(包括n)个字符付给当前字符串
str5.assign("hello C++",5);
cout << "str5= " << str5 << endl;//输出hello
string str6;
//把字符串s付给当前字符
str6.assign(str5);
cout << "str6= " << str6 << endl;//输出hello
string str7;
//用10个字符w组成的字符串给当前字符串赋值
str7.assign(10, 'w');
cout << "str7= " << str7 << endl;
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
一般用等号的方式进行赋值。
4.string字符串拼接+=,append
#include<iostream>
using namespace std;
//string字符串拼接
void test01()
{
string str1 = "我";
//追加字符串,const char*
str1 += "爱你";
cout << "str1= " << str1 << endl;
//追加字符串 string& str
string str2 = " love is love";
str1 += str2;
cout << "str1= " << str1 << endl;
//追加字符 const char c
str1 += '!';
cout << "str1= " << str1 << endl;
string str3 = "I";
//const char* s
str3.append(" Love");
cout << "str3= " << str3 << endl;
//const char* s,的前n个
str3.append(" You abcd",4);
cout << "str3= " << str3 << endl;
//追加字符串 string& str
str3.append(str2);
cout << "str3= " << str3 << endl;
//从pos开始 截取n个字符
string str4 = "abcdef";
string str5 = "ghopq";
str4.append(str5, 2, 3);
cout << "str4= " << str4 << endl;
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
5.string查找和替换,find,replace
#include<iostream>
using namespace std;
//string字符串查找和替换
//1.查找
void test01()
{
string str1 = "abcdabc";
//返回要查找的字符串在str1中第一次出现的位置,查找不到,返回-1
int pos1 = str1.find("de");
int pos2 = str1.find("ab");
cout << "pos1 = " << pos1 << endl;//输出:-1
cout << "pos2 = " << pos2 << endl;//输出:0
//rfind
//rfind从右往左找,计算下标还是从左往右
//即rfind是输出要查找的字符串在str1中最后一次出现的位置
int pos3 = str1.rfind("de");
int pos4 = str1.rfind("ab");
cout << "pos3 = " << pos3 << endl;//输出:-1
cout << "pos4 = " << pos4 << endl;//输出:4
}
//2.替换
void test02()
{
string str1 = "abcdefg";
//从1号位置起的3个字符替换为字符串"1111"
str1.replace(1, 3, "1111");
cout << "str1 = " << str1 << endl;//输出str1 = a1111efg
}
int main()
{
test01();
test02();
system("pause");//按任意键继续
return 0;
}
需要用整数来接收find,直接输出 str1.find("de"),若查找不到,会输出一个很大的数。
6.string字符串比较,compare
#include<iostream>
using namespace std;
//字符串比较
void test01()
{
string str1 = "hello";
string str2 = "xello";
if (str1.compare(str2) == 0)
{
cout << "str1 等于 str2" << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1 大于 str2" << endl;
}
else if (str1.compare(str2) < 0)
{
//x>h
cout << "str1 小于 str2" << endl;
}
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
主要用于判断两个字符串是否相等,判断谁大谁小意义不大。
7.string字符存取[].at,取,修改单个字符,size返回字符串长度
#include<iostream>
using namespace std;
//字符串存取
void test01()
{
string str = "hello";
cout << "str = " << str << endl;
//1.通过[]访问单个字符
//size返回字符串长度
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
//2.通过at方式访问单个字符
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i)<< " ";
}
cout << endl;
//修改单个字符
str[0] = 'x';
cout << "str = " << str << endl;//输出:xello
str.at(1) = 'x';
cout << "str = " << str << endl;//输出:xxllo
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
8.string插入和删除,insert,erase
int main()
{
string s1 = "abcd";
s1.insert(1, "222");
cout << s1 << endl; // a222bcd
s1 = "abcd";
s1.insert(2,2, 'q');
cout << s1 << endl; // abqqcd
s1 = "abcd";
s1.erase(1,2);
cout << s1 << endl; // ad
system("pause");
return 0;
}
9.string截取子串,substr
#include<iostream>
using namespace std;
//string截取子串
void test01()
{
string str = "abcdef";
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl;//subStr = bcd
}
//实用操作
void test02()
{
string email = "zhangsan@sina.com";
//从邮件地址中 获取 用户名信息
int pos= email.find('@');
string use_name = email.substr(0, pos);
cout << "use_name = " << use_name << endl;// use_name = zhangsan
}
int main()
{
test01();
test02();
system("pause");//按任意键继续
return 0;
}