String容器

本文详细介绍了C++中字符串的各种操作,包括构造函数、赋值(使用=操作符和assign方法)、拼接(通过+=操作符和append方法)、查找与替换(find和replace方法)、比较(使用compare方法)、字符存取([]和at()方法)、以及插入和删除(insert和erase方法)。这些基本操作是理解和使用C++字符串的关键。
摘要由CSDN通过智能技术生成

目录

1. 构造函数

2. 字符串赋值

3. 字符串拼接

4. 字符串查找和拼接

5. 字符串比较

6. 字符存取

7. 字符串插入和删除


1. 构造函数

C++98有以下主要构造函数

//string的构造函数
void test01()
{
	//1.默认创造
	string s1;
	//2.字符串构造		
	string s2("hello,world");
	cout << s2 << endl;
	//3.拷贝构造
	string s3(s2);
	cout << s3 << endl;
	//4.字符构造
	string s4(10, 'a');
	cout << s4 << endl;
	//5.字符串构造
	string s5("hello,world", 5);
	cout << s5 << endl;
	//6.拷贝构造
	//从下标0开始拷贝5个字符的字符串
	string s6(s2, 0, 5);
	cout << s6 << endl;
}
int main()
{
	test01();
	return 0;
}

2. 字符串赋值

字符串赋值有两种操作符:

1. =操作符

#include"String.h"

void test01()
{
	//赋值操作
	//1. = 操作符

	//1. 字符串赋值
	string s1;
	s1 = "hello,world";
	cout << "s1 = " << s1 << endl;
	//2. string类赋值
	string s2;
	s2 = s1;
	cout << "s2 = " << s2 << endl;
	//3. 字符赋值
	string s3;
	s3 = 'a';
	cout << "s3 = " << s3 << endl;

}
int main()
{
	test01();
	return 0;
}

2. assign方法

void test02()
{
	//assign方法
	
	//1.字符串赋值
	string s1;
	s1.assign("hello,world");
	cout << "s1 = "<< s1 << endl;
	s1.assign("hello,world", 5);
	cout << "s1 = " << s1<< endl;
	//2.类赋值
	string s2;
	s2.assign(s1);
	cout << "s2 = " << s2 << endl;
	//赋值一部分
	s2.assign (s1, 0, 3);//从下标0开始,取3个字符的字符串进行赋值
	cout << "s2 = " << s2 << endl;
	//字符赋值
	string s3;
	s3.assign(10,'a');
	cout << "s3 = " << s3 << endl;
}
int main()
{
	test02();
	return 0;
}

 3. 字符串拼接

1. +=操作符

//字符串拼接操作
void test01()
{
	//1.字符串拼接
	string s1 = "I";
	s1 += " love ";
	cout << "s1 = " << s1 << endl;
	//2.字符拼接
	s1 += 'U';
	cout << "s1 = " << s1 << endl;
	//3. 类拼接
	string s3 = " zhangsan";
	s1 += s3;
	cout << "s1 = " << s1 << endl;
}
int main()
{
	test01();
	return 0;
}

 2. append方法拼接

         

//字符串拼接操作
void test02()
{
	//1.字符串拼接
	string s1 = "小明";
	s1.append(" 喜欢玩 ");
	cout << "s1 = " << s1 << endl;
	s1.append("LOL DNF", 3);//拼接当前字符串的前3个字符
	cout << "s1 = " << s1 << endl;
	//2.类拼接
	string s2 = "小张和";
	s2.append(s1);
	cout << "s2 = " << s2 << endl;
	
	string s3 = "小张";
	s3.append(s1,5,100); 
//从下标为5开始取100个字符,如果取的字符数大于字符串长度,则取的是字符串下标为5后的全部字符
	cout << "s3 = " << s3 << endl;

	//3.字符拼接
	string s4 = "hh";
	s4.append(10, 'h');
	cout << "s4 = " << s4 << endl;
}
int main()
{
	test02();
	return 0;
}

4. 字符串查找和拼接

1. find方法

void test01()
{
	//1.查找字符串
	string s1 = "abdcdefgde";
	cout <<"\"de\"第一次出现的位置 = "<< s1.find("de") << endl;
	//从下标为0开始,寻找查询子串中的第一个字符的第一次出现位置
	cout <<"\"de\"中的'd'第一次出现的位置 = "<< s1.find("de", 0, 1) << endl;
	//查找字符
	cout << "'g'第一次出现的位置 = "<< s1.find('g') << endl;
	//查找类
	string s2 = "def";
	cout << "s2第一次出现的位置 = " << s1.find(s2) << endl;
}
int main()
{
	test01();
	return 0;
}

rfind与find语法类似,区别在于find是从左往右进行查找,而rfind是从右往左进行查找

2. replace方法

这里我们只考虑替换类型是字符串或者是类

void  test02()
{
	//2.替换
	string s1 = "zhangsan";
	//从下标0位置起,5个字符被替换成了"lisi"
	s1.replace(0, 5, "lisi");
	cout << s1 << endl;
	string s2 = "wangwu";
    //从下标0开始,8个字符被替换成s2中的字符串
	s1.replace(0, 8, s2);
	cout << s1 << endl;
}
int main()
{
	test02();
	return 0;
}

5. 字符串比较

compare方法

类似于C语言中的strcmp函数

void test01()
{
	string s1 = "hello,world";
	string s2 = "hello";
	if (s1.compare(s2) == 0)
	{
		cout << "s1==s2" << endl;
	}
	else if (s1.compare(s2)>0)
	{
		cout << "s1 > s2" << endl;
	}
	else
	{
		cout << "s1 < s2" << endl;
	}
}
int main()
{
	test01();
	return 0;
}

6. 字符存取

既然学习了这么多的操作后,我们如果操作字符串中的单个字符呢?

1. [ ]操作符

void test01()
{
	string s1 = "hello";
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
        s[i]=i+1;
	} 
    for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}


}
int main()
{
	test01();
	return 0;
}

2. at()方法


void test01()
{
	string s1 = "hello";
	for (int i = 0; i < s1.length(); i++)
	{
		cout << s1.at(i) << " ";
        s1.at(i) = i+1;
	}
    for (int i = 0; i < s1.length(); i++)
	{
		cout << s1.at(i) << " ";
	}

}
int main()
{
	test01();
	return 0;
}

7. 字符串插入和删除

1. insert方法

void test01()
{
	string s1 = "hello";
	//1.插入操作
	//在下标为5插入字符串"world"
	s1.insert(5, "world");
	cout << "s1 = "<< s1 << endl;
}
int main()
{
	test01();
	return 0;
}

2. erase方法

void test01()
{
	string s1 = "hello";
	//1.插入操作
	s1.insert(5, "world");
	cout << "s1 = "<< s1 << endl;
	//2.删除操作
	//从下标为5的位置开始删除两个字符
	s1.erase(5, 2);
	cout << "s1 = " << s1 << endl;

	//插入和删除的起始下标都是0
}
int main()
{
	test01();
	return 0;
}

8.子串获取

substr方法

void test01()
{
	string s1 = "hello,world";
	//从下标0开始截取5个字符赋给s2
	string s2 = s1.substr(0, 5);
	cout << s2 << endl;
}
int main()
{
	test01();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值