[C/C++]_[初级]_[标准字符串std::string 所有函数的用法总结]

 
 

场景:

void TestStringAppend()
{
	std::string str;
	const char buff[] ="const char arr";
	//1Appends count copies of character ch
	str.append(1,0x0);
	cout<<"str1:"<<str<<endl;
	//2append a string
	str.append("aaaaabbbbbbii");
	cout<<"str2:"<<str<<endl;
	//3 Appends a substring [pos, pos+count) of str
	str.clear();
	str ="123456789";
	str.append(str,4,15);
	cout<<"str3:"<<str<<endl;
	//4Appends the first count characters of character
	// string pointed to by s. s can contain null characters.
	str.clear();
	str ="123456789";
	//str.append("456",3);//count 表示要从字符串中提取多少个字符
	str.append(1,'4').append(buff,5);
	cout<<"str4:"<<str<<endl;
	//5Appends the null-terminated character string pointed to by s.
	// The length of the string is determined by the first null character. 
	str.clear();
	str ="123456789";
	const char *pstr ="pstr";
	str.append(pstr);
	cout<<"str5:"<<str<<endl;
	//6Appends characters in the range [first, last). 
	str.clear();
	str ="123456789";
	
	str.append(&buff[2],std::end(buff));
	cout<<"str6:"<<str<<endl;
	//7 Appends characters in the initializer list ilist. 
	str.clear();
	str ="123456789";
	//str.append({' ', 'l', 'i', 's', 't'});//c++ prime 11
	cout<<"str7:"<<str<<endl;

}

运行结果:



场景:

void TestStingCompare()
{
	std::string str ="test string ";
	std::string first_str ="aaaaa";
	std::string second_str ="bbbbb";
	const char* pstr ="caaaat char pstr";

	int flag =first_str.compare(second_str);
	cout<<"flag:"<<flag<<endl;
	flag =str.compare(pstr);
	cout<<"flag:"<<flag<<endl;
	flag =str.compare(1,3,first_str);
	cout<<"flag:"<<flag<<endl;
	flag =str.compare(2,1,pstr,3);
	cout<<"flag:"<<flag<<endl;
	flag =str.compare(2,1,first_str,4,1);
	cout<<"flag:"<<flag<<endl;

}
运行结果:


场景:

void TestStringReplace()
{
	std::string str ="E:\\aaa\\bbbb\\ccc\\11.png";
	std::string temp_str ="bbbb";
	const char* pstr ="aaa";

	str=str.replace(2,4,temp_str);//从第二个字符之后开始,3-6等四个字符替换temp_str从第一个位置开始的四个字符
	cout<<"str replace:"<<str<<endl;

	str=str.replace(3,4,6,'f');//从第3个字符到第4个字符之间的字符,替换为6个f
	cout<<"str replace:"<<str<<endl;

	str=str.replace(2,5,pstr,6);//从第二个字符到第4个字符之间的字符,替换pstr从第一个位置开始的6个字符
	cout<<"str replace:"<<str<<endl;

	str=str.replace(3,4,temp_str,4,7);//从第3字符到第4个字符之间的字符,替换pstr从第4字符到第7个字符之间的字符
	cout<<"str replace:"<<str<<endl;

	str=str.replace(str.begin()+5,str.end(),"replace");//把一定范围内的字符替换为replace
	cout<<"str replace:"<<str<<endl;

	str=str.replace(str.begin()+5,str.end(),"num",7);//把一定范围内的字符替换为大小为7 的字符串,如果字符串大小不够用空格填充
	cout<<"str replace:"<<str<<endl;

	str=str.replace(str.begin()+7,str.end(),4,'*');//把一定范围内的字符替换为4个*
	cout<<"str replace:"<<str<<endl;
        
        //算法库:
        replace(str.begin(),str.end(),'i',';');//(char)0x20);
        cout<<"str:"<<str<<endl;

 }

运行结果:


场景:

void TestStringInsert()
{
	std::string str ="E:\\aaa\\bbbb\\ccc\\11.png";
	std::string temp_str ="bbbb";
	const char* pstr ="aaa";
	temp_str=temp_str.insert(3,pstr); //从temp_str第三个字符后面插入字符串pstr
	str=str.insert(4,temp_str);
	cout<<"temp_str insert:"<<temp_str<<endl;
	cout<<"str insert:"<<str<<endl;

	str=str.insert(3,pstr,4);//str第三个字符后面插入长度为4的字符串pstr,如果大小不够,用空格填充
	cout<<"str insert:"<<str<<endl;

	str=str.insert(4,5,'o');//从str第4个字符后面插入5个字符o
	cout<<"str insert:"<<str<<endl;

	str=str.insert(1,temp_str,4,5);//从str第1个字符后面字符串范围4-5之间的字符
	cout<<"str insert:"<<str<<endl;
	
	str.insert(str.begin(),3,'f');//从str开始位置插入3个字符f
	cout<<"str insert:"<<str<<endl;

	str.insert(str.begin(),str.begin()+4,str.end());//从str.begin()开始位置插入一定范围的字符串【str.begin()+4 str.end()】
	cout<<"str insert:"<<str<<endl;
}

运行结果:


场景:

//13getline
	str ="aaa|bbb|ccc|ddd|eee";
	std::stringstream ss(str);
	std::string ss_str;
	while(getline(ss,ss_str,'|'))
	{
		cout<<"ss_str:"<<ss_str<<endl;
	}

运行结果:



场景:

void TestStringAssign()
{
	std::string str ="test string ";
	std::string first_str ="abcdefghijk";
	std::string second_str ="bbbbb";
	const char* pstr ="const char pstr";
	
	str =str.append(first_str);
	cout<<"assign str:"<<str<<endl;
	str =str.assign(pstr); //pstr 赋值给str;
	cout<<"assign str:"<<str<<endl;
	str.clear();
	str =str.assign(pstr,4); //获取pstr前面四个字符
	cout<<"assign str:"<<str<<endl;
	str.clear();
	str =str.assign(first_str,3,7);//提取从第三个字符开始后四个字符
	cout<<"assign str:"<<str<<endl;

	const char ch_g = 'g';
	str =str.assign(6,ch_g); //6个字符赋值给str
	cout<<"assign str:"<<str<<endl;

	//str.clear();
	const char ch = 'w';
	str.assign(9,ch); //字符串赋值9个字符
	cout<<"assign str:"<<str<<endl;
	str.assign(str.begin()+4,str.end());
	cout<<"assign str:"<<str<<endl;

}

运行结果:


场景:

void TestStringErase()
{
	std::string str ="E:\\aaa\\bbbb\\ccc\\11.png";
	int index = str.find_last_of('\\');
	//str.erase(index);
	//str.erase(0,index+1);
	str.erase(str.begin()+index,str.end());
	cout<<"str erase:"<<str<<endl;

        //算法库
        wstring str(L"1-63-42-5 375-2-3");
        str.erase(remove(str.begin(),str.end(),L'-'),str.end());
 }

运行结果:


场景:

void TestSubstr()
{
	// C++ substr
	std::string str ="E:\\aaa\\bbbb\\ccc\\11.png";
	int sub_index =str.find_last_of('\\');
	std::string sub_str =str.substr(0,sub_index);
	cout<<"str substr:"<<sub_str<<endl;

	//C strrchr 字符串向后查找
	char* cc = strdup(str.c_str());
	char* c_str =  strrchr(cc,'\\');
	if(c_str)
	{
		*(c_str+1) = 0;
	}
	cout << "cc: " << cc << endl;
}


运行结果:



场景:

#include <stdio.h>
#include <string>
#include <vector>
#include<iostream>
#include <sstream>
#include <algorithm>
#include <Windows.h>
using namespace std;
void TestStrAllFunction()
{
	std::string str ="test string ";
	std::string first_str ="abcdefghijk";
	std::string second_str ="bbbbb";
	
	//4 at 下标从0开始,返回一个字符
	char ch =str.at(2);
	cout<<"str at:"<<ch<<endl;
	//5 begin end
	cout<<"str begin:"<<(*str.begin())<<endl;
	cout<<"str end:"<<(*(str.end()-1))<<endl;//str.end()字符串最后一个元素的下一个
	//6 c_str data
	//std::string to const char *
	const char* temp_char =str.c_str();
	cout<<"str char*:"<<temp_char<<endl;
	const char* data_str =str.data();
	cout<<"str Data:"<<temp_char<<endl;
	//7 capacity  max_size size legth
	int count =str.capacity();
	vector<int> vect(10);
	vect.reserve(30);
	int vect_count =vect.capacity();
	int size = str.size();
	int length = str.length();
	int max_size = str.max_size();
	cout<<"count:"<<count<<",vect_count:"<<vect_count
	<<",size:"<<size<<", length:"<<length
	<<"max_size:"<<max_size<<endl;
	
	//9copy 可以把指定长度的字符串拷贝出来,而不必切割字符串,提取指定长度,还减少了一个类型转换std::string to const char*
	char buff[128];
	memset(buff,'\0',128);
	str.copy(buff,20);
	cout<<"buff:"<<buff<<endl;
	//10 empty
	int empty_flag =str.empty();
	cout<<"empty_flag:"<<empty_flag<<endl;
	//11 erase
	

	vector<int> temp_vector;
	for(int i = 0; i<10;++i)
	{
		temp_vector.push_back(i);
	}
	vector<int>::iterator it;
	for(it =temp_vector.begin();it != temp_vector.end();++it)
	{
		cout<<"it:"<<(*it)<<"\t";
	}

	//16rbegin  rend
	for (std::string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
	{
	  cout <<"*rit:"<< *rit<<"\t";
	}
	
	//20 swap
	first_str ="aaaaa";
	second_str ="bbbbb";
	first_str.swap(second_str);
	cout<<"first_str:"<<first_str<<"\n"
	<<"second_str:"<<second_str<<endl;
}

int main(int agrc,char*agrv[])
{
	TestStrAllFunction();
	//TestSubstr();
	system("pause");
	return 0;
}                           

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值