本文介绍了如何在C ++中的字符串中每N个字符插入一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在一个字符串字符之后插入字符

How can I insert a character into a string exactly after 1 character?

换句话说,(C ++):Tokens all around!

成为:T | o | k | e | n | s | | a | l | l | | a | r |

In other words (C++): "Tokens all around!"
Turns into: "T|o|k|e|n|s| |a|l|l| |a|r|o|u|n|d|!" (no thats not an array)

感谢

推荐答案

std::string tokenize(const std::string& s) {
   if (!s.size()) {
     return "";
   }
   std::stringstream ss;
   ss << s[0];
   for (int i = 1; i < s.size(); i++) {
     ss << '|' << s[i];
   }
   return ss.str();
}

这篇关于如何在C ++中的字符串中每N个字符插入一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:18
查看更多