How do you get a sub-line from the index?



  • There's an index.

    char *s="n1=1&n2=2&name=test&sername=test2";
    

    We need a line. name take into account, for example, test and put it in another variable. We need to find a point between us. name = & and the following &



  • For example, you can do this in a way:

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

    int main()
    {
    const char *s = "n1=1&n2=2&name=test&sername=test2";
    const char *t = "name=";
    std::string value;

    if ( const char *p = std::strstr( s, t ) )
    {
        p += std::strlen( t );
        const char *q = std::strchr( p, '&amp;' );
        q == nullptr ? value.assign( p ) : value.assign( p, q - p );
    }
    
    std::cout &lt;&lt; "value = \"" &lt;&lt; value &lt;&lt; '\"' &lt;&lt; std::endl;
    
    return 0;
    

    }

    Programme withdrawal:

    value = "test"

    We can also use a search line "&name="♪ In this case, only one such substructure will be found in the reference line.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2