c++字符串string操作全解

#pragma once                  //防止文件被多重包含
#include <stdio.h>
#include <stdlib.h>
#include <string>             //字符串
#include <iostream>           //数据流
#include <memory>             //内存操作
#include <regex>              //正则表达式
#include <chrono>             //正则表达式   vs2012后有

using namespace std;


int main()
{
	string strlp="luanpeng";
	char* charlp = "luanpeng";
	char chararrlp[] = "luanpeng";

	//char*和char[]转str,直接赋值
	strlp=charlp;
	strlp=chararrlp;

	//str转char*
	const char* charlp1 = strlp.data();   //data返回不带\n,c_str返回带\n。
	strcpy(charlp,strlp.c_str());  //c_str()并不新建内存,c_str()和data只能转换为const的变量。strcpy为c语言复制内存函数

	//str转char[]
	char p[5];
	strlp.copy(p,2,1);  //2表示赋值的长度,1表示开始位置
	p[2]='\n';   //手动添加'\n'


}

//字符串转化大小写
string lower(string str)
{
	transform(str.begin(),str.end(),str.begin(),tolower);
	return str;
}

//字符串转化大小写
string upper(string str)
{
	transform(str.begin(),str.end(),str.begin(),toupper);
	return str;
}

//去除字符串的首尾
void trim_string(string &s)     //字符串去除首尾
{  
	if (!s.empty())   
	{  
		s.erase(0,s.find_first_not_of(" "));  
		s.erase(s.find_last_not_of(" ") + 1);  
	}
}

//按字符分割字符串
void split_char(const std::string& yuan, std::vector<std::string>& arr, const std::string& split)   //多字符分割
{
	std::string::size_type pos1, pos2;
	pos2 = yuan.find_first_of(split);
	pos1 = 0;
	while(std::string::npos != pos2)
	{
		if (pos1!=pos2)
			arr.push_back(yuan.substr(pos1, pos2-pos1));
		pos1 = pos2 + 1;
		pos2 = yuan.find_first_of(split, pos1);   //find_first_of方法查询任意一个字符首次出现的位置,允许了使用多个分割字符
	}
	if(pos1 != yuan.length())  //分割剩余的最后一个字符串
		arr.push_back(yuan.substr(pos1));
}

//按字符串分割字符串
void split_string(const std::string& yuan, std::vector<std::string>& arr, const std::string& split)   //字符串分割
{
	std::string::size_type pos1, pos2;
	pos2 = yuan.find(split);
	pos1 = 0;
	while(std::string::npos != pos2)
	{
		if (pos1!=pos2)
			arr.push_back(yuan.substr(pos1, pos2-pos1));
		pos1 = pos2 + split.size();
		pos2 = yuan.find(split, pos1);   //find_first_of方法查询任意一个字符首次出现的位置,允许了使用多个分割字符
	}
	if(pos1 != yuan.length())
		arr.push_back(yuan.substr(pos1));
}

//替换全部子字符串
void  replace_string(string&   str,const   string&   old_value,const   string&   new_value)   
{   
	for(string::size_type   pos(0);   pos!=string::npos;   pos+=new_value.length())   {   
		if(   (pos=str.find(old_value,pos))!=string::npos   )   
			str.replace(pos,old_value.length(),new_value);   
		else   break;   
	}   
}

//字符串长度
int length(string& str)
{
	return str.length();
}

//连接两个字符串,每个字符串最右边的空格被裁切 
string strcat(string& a,string& s) 
{
	string aa = a;
	string ss=s;
	aa.erase(aa.find_last_not_of(" ") + 1);  
	ss.erase(ss.find_last_not_of(" ") + 1);  
	return aa+ss;
}

//比较字符串是否相等
bool strcmp(string& a,string& b)
{
	if (a==b)
		return true;
	return false;
}

//比较字符串前n个字符相等
bool strncmp(string& a,string& b,int n)
{
	string aa=a.substr(0,n);
	string bb=b.substr(0,n);
	if (aa==bb)
		return true;
	return false;
}

//比较字符串是否相等,不区分大小写
bool strcmpi(string& a,string& b)
{
	string aa=lower(a);
	string bb=lower(b);
	if (aa==bb)
		return true;
	return false;
}

//比较字符串前n个字符相等,不区分大小写
bool strncmpi(string& a,string& b,int n)
{
	string aa=a.substr(0,n);
	string bb=b.substr(0,n);
	aa=lower(aa);
	bb=lower(bb);
	if (aa==bb)
		return true;
	return false;
}

//检测字符串中每个字符时否属于英文字母 
bool isletter(string& a)
{
	for (int i=0;i<a.length();i++)
	{
		if(a[i]<'a' || a[i]>'Z')
			return false;
	}
	return true;
}

//检测字符串中每个字符是否属于格式字符(空格,回车,制表,换行符等) 
bool isspace(string& a)
{
	for (int i=0;i<a.length();i++)
	{
		if(a[i]!=' ' && a[i]!='\n' && a[i]!='\t' && a[i]!='\r')
			return false;
	}
	return true;
}

//删除结尾空格
void deblank(string& a)
{
	a.erase(a.find_last_not_of(" ") + 1);  
}

//裁切字符串的开头和尾部的空格,制表,回车符
void strtrim(string& str)
{
	trim_string(str);
}

//替换子串
string strrep(string s,string& sub,string& replace)
{
	replace_string(s,sub,replace);
	return s;
}

//分割字符串成两段
string* strtok(string s,char split)
{
	string* p=new string[2];
	std::string::size_type pos1=s.find(split);
	if (pos1==std::string::npos)
	{
		p[0]=s;
		p[1]="";
	}else
	{
		p[0]=s.substr(0,pos1);
		p[1]=s.substr(pos1+1);
	}
	return p;

}

//创建有n个空格组成的字符串 
string blanks(int n)
{
	string p="";
	for(int i=0;i<n;i++)
		p=p+" ";
	return  p;
}

//查找str1和str2中,较短字符串在较长字符串中出现的位置,没有出现返回空数组 
vector<int> findstr(string& s,string& sub)
{
	vector<int> all;
	std::string::size_type pos1=s.find(sub);
	if (pos1!=std::string::npos)
	{
		all.push_back(pos1);
		pos1=s.find(sub);
	}
	return all;
}

//将数字转换为数字字符串 
string num2str(double value)
{
	return to_string(value);
}

//把数值数组转换为整数数字组成的字符数组 
string int2str(int value)
{
	return to_string(value);
}

//将数字字符串转换为数字 
double str2num(string str)
{
	return stod(str);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾讯AI架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值