c++ string 切分
时间: 2023-08-18 12:07:33 浏览: 99
针对C++中对string的切分的问题,可以使用两种方法来实现。
方法一:可以使用C语言中的strtok函数来进行分割。strtok函数包含在头文件<string.h>中,对于字符数组可以采用这种方法处理。测试代码如下:
```
#include <cstdio>
#include <cstdlib>
#include <cstring>
int main() {
std::string str = "a,b*c,d";
const char *sep = ",*"; // 可按多个字符来分割
char *p;
p = strtok((char*)str.c_str(), sep);
while (p) {
printf("%s ", p);
p = strtok(NULL, sep);
}
printf("\n");
system("pause");
return 0;
}
```
<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* *2* [C++ string字符串分割](https://blog.csdn.net/qq_38616559/article/details/119610516)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
- *3* [C++如何切割String对象](https://blog.csdn.net/sxycylq/article/details/124107494)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
[ .reference_list ]
阅读全文