一个C++字符串替换和截取的实例
来源:互联网 发布:便宜mp3知乎 编辑:程序博客网 时间:2024/06/06 08:51
一个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;}
运行结果:
0 0
- 一个C++字符串替换和截取的实例
- 字符串的截取和替换
- Objective-C 【NSRange&字符串的截取和替换】
- 字符串截取和替换
- 批处理中字符串的截取和替换
- OC字符串的截取和替换
- python字符串的截取和替换
- NSString 字符串的截取和替换
- java 字符串截取和替换
- 字符串替换的一个工作实例
- 字符串进行截取替换操作实例代码
- js字符串的截取,替换
- 批处理中字符串的截取和替换 (转载)
- Linux 的变量字符串截取和替换方法
- 正则表达式-字符串基本的匹配,拆分,替换和截取
- c和c++中字符串的截取
- 有关SQL 截取和替换字符串
- 在命令行中的替换和截取字符串
- C++ 数组到底能开多大。。
- extjs创建弹窗输入框
- 漏洞修复之 bash漏洞远程命令执行
- Android开发优化之对界面UI的优化
- 如何在网页中嵌入自己想要的字体
- 一个C++字符串替换和截取的实例
- Mybatis查询之list作为参数查询
- Description Resource Path Location Type Java compiler level does not match the version of(图解)
- c++中string 的replace用法
- 移码的再总结
- 对Rxjava的理解以及结合Retrofit使用(一)
- Sublime Text无法使用Emmet插件(附带手动安装)
- Centos下Valgrind使用与安装
- qduoj 韬韬说做出这个泥萌就AK辣