一个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;
}
运行结果: