How to split and iterate a string separated by a specific character in C++?

How to split and iterate a string separated by a specific character in C++? For example,

"a string   separated by space" by ' '=>
["a", "string", "separated", "by", "space"]

and

"a,string,separated,by,comma" by ',' =>
["a", "string", "separated", "by", "comma"]

C++ standard library’s getline() function can be used to build a function. getline() can accept a delimiter character.

template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT,Traits>& getline(
  std::basic_istream<CharT,Traits>& input,
  std::basic_string<CharT,Traits,Allocator>& str,
  CharT delim
);

Then the string vector can be iterated using common ways such as range-based for loop.

Here is a C++ function to split the string into a string vector by the delimiter character.

#include <string>
#include <sstream>
#include <vector>

std::vector<std::string> split(const std::string str, char delim)
{
  std::vector<std::string> result;
  std::istringstream ss{str};
  std::string token;
  while (std::getline(ss, token, delim)) {
    if (!token.empty()) {
      result.push_back(token);
    }
  }
  return result;
}

A full C++ function is as follows.

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

std::vector<std::string> split(const std::string str, char delim)
{
  std::vector<std::string> result;
  std::istringstream ss{str};
  std::string token;
  while (std::getline(ss, token, delim)) {
    if (!token.empty()) {
      result.push_back(token);
    }
  }
  return result;
}

int main ()
{
  auto v1 = split(std::string{"a string   separated by space"}, ' ');

  std::cout << "Split v1:" << std::endl;
  for (auto &s : v1) {
    std::cout << s << std::endl;
  }

  auto v2 = split(std::string{"a,string,separated,by,comma"}, ',');

  std::cout << "---------------\nSplit v2:" << std::endl;
  for (auto &s : v2) {
    std::cout << s << std::endl;
  }

  return 0;
}

The execution result is as follows.

$ ./build/main 
Split v1:
a
string
separated
by
space
---------------
Split v2:
a
string
separated
by
comma

Similar Posts

  • Chinese Charactor Configuration on Fedora 11

    最新的更新版本请看: Fedora 中文字体设置. 使用Linux时我个人倾向使用英文环境系统,而Fedora11在英文环境下中文字体有时会不太好看,经常遇到需要字体优化美化的问题。 以下是我的配置方案,经测试效果还算不错,解决了Fedora 11 中文字体难看的问题: 方案1:使用uming和ukai字体,即AR PL UMing CN等。 关键是使用的字体包如下: 首先要安装这两个字体: cjkuni-ukai-fonts cjkuni-uming-fonts 然后配置一下~/.fonts.conf文件. 使sans-serif serif monospace字体中文使用uming/ukai即可. 我的.fonts.conf文件可以从这里下载(两种选择, 我喜欢前者): https://github.com/zma/config_files 使用Liberation和uming/ukai字体: .fonts.cofn.liberation 使用dejavu和uming/ukai字体: .fonts.conf.dejavu 下载后放到自己的$HOME下改名为.fonts.conf就可以了。 使用uming字体效果如下(请放大后看效果): 方案2:安装文泉驿字体,这个非常简单,安装相应包即可了。 如果喜欢其它的字体选择性的安装上就可以了,只要注意只安装自己需要的就行了。有人使用微软雅黑字体,首先这是侵权的,其次开源的字体做得其实已经很不错了。 最后将字体平滑选项打开, KDE和gnome都有相关设置方法。 以上内容只是针对使用xft字体系统的设置。对于使用核心字体系统的X程序来说字体依然会出现很丑的情况。 下面是针对emacs的设置方法: 首先需要安装这个字体包: xorg-x11-fonts-misc 注意到在中文系统下emacs的中文显示非常好,而在英文环境中去非常差,我们可以利用这一点,在运行emacs前首先将系统环境设为中文即可。 在~/bin/下建立一文件ema 内容如下: #!/bin/bash rm -f ~/.emacs ln -s ~/.emacs.x ~/.emacs LANG=zh_CN.UTF-8 emacs –fullheight -r $* 然后加入执行权限即可: chmod +x…

  • Any good Java REPL tool/implementation?

    Any good suggestions on a Java REPL implementation like ‘scala’ and ‘python’ or ‘php -a’? The java-repl tool https://github.com/albertlatacz/java-repl/ works nicely for most situations for me. It is released as a .jar. Hence, it is easy to download and run: $ wget –quiet https://github.com/albertlatacz/java-repl/releases/download/428/javarepl-428.jar -O /tmp/javarepo-428.jar && java -jar /tmp/javarepo-428.jar One usage example is as…

  • How to get the git commit tree?

    How to get a tree-like view of the git commit history? My favorite command line: git log –graph –oneline It will prints a text based graph like (with colors): * b5dc8b9 Merge branch ‘master’ of https://github.com/layerzero/libi0 | | * 7514ef1 revised the README.md a little bit | * 6692428 align size to page for both…