寻找路径字符串中特定的子串,例如/home/test/csv/aaa/minieye/xxx.csv,需要截取csv和minieye中间的内容,也就是aaa,使用c++
时间: 2023-08-07 15:03:48 浏览: 48
你可以使用C++中的字符串操作函数来截取路径字符串中特定子串之间的内容。以下是一个示例代码:
```cpp
#include <iostream>
#include <string>
std::string extractSubstring(const std::string& sourceString, const std::string& startSubstring, const std::string& endSubstring) {
size_t startIndex = sourceString.find(startSubstring);
size_t endIndex = sourceString.find(endSubstring);
if (startIndex == std::string::npos || endIndex == std::string::npos) {
return "未找到指定的子串";
}
startIndex += startSubstring.length();
return sourceString.substr(startIndex, endIndex - startIndex);
}
int main() {
std::string pathString = "/home/test/csv/aaa/minieye/xxx.csv";
std::string startSubstring = "csv/";
std::string endSubstring = "/minieye";
std::string extractedString = extractSubstring(pathString, startSubstring, endSubstring);
std::cout << extractedString << std::endl;
return 0;
}
```
在这个例子中,`extractSubstring()` 函数接受三个参数:`sourceString` 是要截取的路径字符串,`startSubstring` 是起始子串("csv/"),`endSubstring` 是结束子串("/minieye")。函数使用 `find()` 函数找到这两个子串在路径字符串中的位置,然后使用 `substr()` 函数截取它们之间的内容。如果找不到指定的子串,则返回一个提示信息。
你可以根据你的需求修改 `pathString`、`startSubstring` 和 `endSubstring` 的值,并调用 `extractSubstring()` 函数来提取你想要的内容。在这个例子中,输出将会是 "aaa"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)