从字符串中删除特定子字符串

时间:2015-07-14 07:28:34

标签: c++ string replace

我想从消息中删除笑脸。我用这个标签<sml/>定义了笑脸,我绝对给出了每个标签的具体数字......如<sml1/><sml2/>,...,<sml30/>。它可以是特定字符串的子字符串,如:

以下是邮件<sml4/>

这里<sml4/>是消息。

<sml4/>这是消息。

我想从邮件中删除此标记。结果将是: 这是消息。

std::string receivedMessage = msg.body();
    if (receivedMessage.find("<sml") != std::string::npos && receivedMessage.find("/>") != std::string::npos)
    {
       for(int i=0 ; i<=30 ; ++i)
        receivedMessage = receivedMessage.remove ("<sml".i."/>")
    }

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

使用C ++ 11,假设你有:

string message = R"Here is messages <sml4/>";

你可以写一个简单的Amit(正如评论中boost已经建议的那样)。把它放在代码中:

string messageWithoutSmiles = regex_replace(message,
    regex(R"<\s*sml\d+\s*\/\s*>"), "");

正则表达式非常简单,但有一点解释可能会有所帮助:

  • <\s* &lt; 字符后跟零个或多个空格。
  • sml文字字符串。
  • \d+后跟一个或多个数字(相当于[0-9])。
  • \s*后跟零个或多个空格。
  • \/\s* / 字符后跟零个或多个空格。
  • >关闭&gt;

如果C ++ 11不是一个选项(!)并且您已经在使用{{3}},那么您将拥有一个等效的正则表达式工具。只是为了好玩你也可以手动实现类似的东西(没有空格来简化代码,远离性能POV最优,处理空格只需在原子中添加更多.find()分裂标记单位&lt; sml / &gt; ,可能有一个方便的帮助函数):

while (true) {
    const string::size_type n1 = message.find("<sml", 0);
    if (n1 == string::npos)
       break;

    const string::size_type n2 = message.find("/>", n1);
    if (n2 == string::npos)
       break;

    message = message.erase(n1, n2 - n1 + 2);
}

<子> 注意:代码不是最优,它甚至不是很好(它应该封装在一个函数中,你不会在现实生活中使用while (true)但是它的目的是说明(然后易于阅读和理解),而不是随时可用。

答案 1 :(得分:2)

不使用正则表达式,例如,如果您的编译器不支持它们,您可以执行此演示程序中显示的任务

#include <iostream>
#include <string>
#include <cstring>

int main()
{
    for ( std::string s : { "Here is messages <sml4/>", "Here <sml4/> is messages", "<sml4/> Here is messages" } )
    {
        const char start[] = "<sml";
        const char end[]   = "/>";
        const size_t l = sizeof( end );

        std::string::size_type n1, n2;

        if ( ( n1 = s.find( start ) ) != std::string::npos && 
             ( n2 = s.find( end, n1 ) ) != std::string::npos ) 
        {
            n2 += l - 1;
            if ( n2 != s.size() && std::isblank( ( unsigned char )s[n2] ) ) ++n2;
            else if ( n1 != 0 && std::isblank( ( unsigned char )s[n1-1] ) ) --n1;

            std::cout << s << std::endl;
            s.erase( n1, n2 - n1 );                     
            std::cout << s << std::endl;
            std::cout << std::endl;
        }
    }                     
}

程序输出类似于

Here is messages <sml4/>
Here is messages

Here <sml4/> is messages
Here is messages

<sml4/> Here is messages
Here is messages

同样为了简单起见,函数std::isblank的调用可以替代以下比较

            if ( n2 != s.size() && s[n2] == ' ' ) ++n2;
            else if ( n1 != 0 && s[n1-1] == ' ' ) --n1;

答案 2 :(得分:-1)

std::string message = "Here is messages < sml4/> ";             
std::size_t found =  message.find("< sml4/> ");

if (found!=std::string::npos)
{                                                     
 message.erase(found,found+sizeof("< sml4/> ")); 

}
相关问题