如何使用stringstream来分隔逗号分隔的字符串

编程入门 行业动态 更新时间:2024-10-27 06:31:27
本文介绍了如何使用stringstream来分隔逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码:

std::string str = "abc def,ghi"; std::stringstream ss(str); string token; while (ss >> token) { printf("%s\n", token.c_str()); }

输出为:

abc def,ghi

abc def,ghi

c> stringstream ::>> 运算符可以按空格分隔字符串,但不能以逗号分隔。是否还有修改上述代码,以便我可以得到以下结果?

So the stringstream::>> operator can separate strings by space but not by comma. Is there anyway to modify the above code so that I can get the following result?

输入:abc ,def,ghi

input: "abc,def,ghi"

输出: abc def ghi

output: abc def ghi

推荐答案

#include <iostream> #include <sstream> std::string input = "abc,def,ghi"; std::istringstream ss(input); std::string token; while(std::getline(ss, token, ',')) { std::cout << token << '\n'; }

更多推荐

如何使用stringstream来分隔逗号分隔的字符串

本文发布于:2023-10-26 22:08:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531488.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逗号   字符串   如何使用   stringstream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!