c++ - Remove specific substring from string -


i remove smiley message. i've defined smiley tags <sml/> , absolutely gave specific number each of them..like <sml1/>, <sml2/> ,..., <sml30/>. can sub string of specific string :

here messages <sml4/>
or
here <sml4/> messages.
or
<sml4/> here messages.

i want remove tag message. , result : here messages.

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."/>")     } 

any suggestion?

without using regular expression example if compiler not support them can task shown in demonstrative program

#include <iostream> #include <string> #include <cstring>  int main() {     ( std::string s : { "here messages <sml4/>", "here <sml4/> messages", "<sml4/> here 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;         }     }                      } 

the program output looks like

here messages <sml4/> here messages  here <sml4/> messages here messages  <sml4/> here messages here messages 

also simplicity calls of function std::isblank can substitute following comparisons

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

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -