一个C++字符串替换和截取的实例

一个C++字符串替换和截取的实例

#include <iostream>
#include <string>


/*************************************************************************
 *@Function: StringReplace
 *
 *@Param1 [IN/OUT]: strBase 传入的是原始字符串,传出的是替换后的字符串
 *
 *@Param2 [IN]: strSrc 待替被换的字符串或者字符
 *
 *@Param3 [IN]: strDst 用于替换的字符串或者字符
 *
 *@Return: 无
 * 
 *@Description:
 *		用一个子串替换原始字符串的子串
 *************************************************************************/
void StringReplace(std::string &strBase, const std::string strSrc, const std::string strDst)
{
	std::string::size_type position = 0;
	std::string::size_type srcLen = strSrc.size();
	std::string::size_type dstLen = strDst.size();

	while ((position = strBase.find(strSrc, position)) != std::string::npos)
	{
		strBase.replace(position, srcLen, strDst);
		position += dstLen;
	}
}



/*************************************************************************
 *@Function: GetFilePathAndFileName
 *
 *@Param1 [IN]: strAbsoluteFilePath 一个文件的绝对路径
 *
 *@Param2 [IN]: strSeparator 文件路径分隔符
 *
 *@Param3 [OUT]: strFilePath 文件路径,不包含文件名
 *
 *@Param4 [OUT]: strFileName 文件名,不包含路径
 *
 *@Return: 无
 * 
 *@Description:
 *		根据文件路径分隔符,将文件的绝对路径分割成文件路径和文件名
 *
 *************************************************************************/
void GetFilePathAndFileName(std::string &strFileAbsolutePath, const std::string strSeparator, 
							std::string &strFilePath, std::string &strFileName)
{
	if (!strSeparator.empty())
	{
		std::string::size_type position = strFileAbsolutePath.rfind(strSeparator);
		if (position != std::string::npos)
		{
			std::string::size_type len = strFileAbsolutePath.length() - position - 1;
			strFilePath = strFileAbsolutePath.substr(0, position);
			strFileName = strFileAbsolutePath.substr(position + 1, len);
		}
	}
}



/*************************************************************************
 *@Function: GetFilePathFromAbsoluteFilePath
 *
 *@Param1 [IN]: strAbsoluteFilePath 一个文件的绝对路径
 *
 *@Param2 [IN]: strSeparator 文件路径分隔符
 *
 *@Return: 成功返回文件路径(不包含文件名),失败返回空字符串
 * 
 *@Description:
 *		根据文件路径分隔符,获取不包含文件名的文件路径
 *
 *************************************************************************/
std::string GetFilePathFromAbsoluteFilePath(std::string &strFileAbsolutePath, const std::string strSeparator)
{
	std::string strFilePath = "";

	if (!strSeparator.empty())
	{
		std::string::size_type position = strFileAbsolutePath.rfind(strSeparator);
		if (position != std::string::npos)
		{
			strFilePath = strFileAbsolutePath.substr(0, position);
		}
	}

	return strFilePath;
}


/*************************************************************************
 *@Function: GetFileNameFromAbsoluteFilePath
 *
 *@Param1 [IN]: strAbsoluteFilePath 一个文件的绝对路径
 *
 *@Param2 [IN]: strSeparator 文件路径分隔符
 *
 *@Return: 成功返回文件名(不包含路径),失败返回空字符串
 * 
 *@Description:
 *		根据文件路径分隔符,获取不包含路径的文件名
 *
 *************************************************************************/
std::string GetFileNameFromAbsoluteFilePath(std::string &strFileAbsolutePath, const std::string strSeparator)
{
	std::string strFileName = "";

	if (!strSeparator.empty())
	{
		std::string::size_type position = strFileAbsolutePath.rfind(strSeparator);
		if (position != std::string::npos)
		{
			std::string::size_type len = strFileAbsolutePath.length() - position - 1;
			strFileName = strFileAbsolutePath.substr(position + 1, len);
		}
	}

	return strFileName;
}



int main()
{
	std::string strFileAbsolutePath = "D:\\ProgramFiles\\Data\\Tool.exe";
	std::string strFilePath;
	std::string strFileName;

	std::cout << "Original AbsoluteFilePath: " << strFileAbsolutePath << std::endl;

	StringReplace(strFileAbsolutePath, "\\", "#");
	GetFilePathAndFileName(strFileAbsolutePath, "#", strFilePath, strFileName);

	std::cout << "Modified AbsoluteFilePath: " << strFileAbsolutePath << std::endl;
	std::cout << "FilePath: " << strFilePath << std::endl;
	std::cout << "FileName: " << strFileName << std::endl;

	return 0;
}

运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值