用多个定界符将字符串分割成单词 [英] Split a string into words by multiple delimiters

查看:90
本文介绍了用多个定界符将字符串分割成单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文本(有意义的文本或算术表达式),我想将其拆分为单词。

如果我有一个定界符,请使用:

I have some text (meaningful text or arithmetical expression) and I want to split it into words.
If I had a single delimiter, I'd use:

std::stringstream stringStream(inputString);
std::string word;
while(std::getline(stringStream, word, delimiter)) 
{
    wordVector.push_back(word);
}

我如何使用几个定界符将字符串分成令牌?

How can I break the string into tokens with several delimiters?

推荐答案

假设其中一个定界符是换行符,则以下内容将读取该行,并用定界符进一步对其进行拆分。在此示例中,我选择了定界符空间,撇号和分号。

Assuming one of the delimiters is newline, the following reads the line and further splits it by the delimiters. For this example I've chosen the delimiters space, apostrophe, and semi-colon.

std::stringstream stringStream(inputString);
std::string line;
while(std::getline(stringStream, line)) 
{
    std::size_t prev = 0, pos;
    while ((pos = line.find_first_of(" ';", prev)) != std::string::npos)
    {
        if (pos > prev)
            wordVector.push_back(line.substr(prev, pos-prev));
        prev = pos+1;
    }
    if (prev < line.length())
        wordVector.push_back(line.substr(prev, std::string::npos));
}

这篇关于用多个定界符将字符串分割成单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆