to check if the given string is substring using c++

Discussion in 'C++' started by varsha.gadekar@gmail.com, Nov 13, 2006.

  1. Guest

    what is the easy way to check if the given string is substring of other
    string only using c++?
     
    , Nov 13, 2006
    #1
    1. Advertising

  2. Guest

    std::string s("Some text is here");
    std::string::size_type pos = s.find("text");
    if (pos == std::string::npos)
    {
    // Not found
    }
    else
    {
    // Substring is on index pos
    }
     
    , Nov 13, 2006
    #2
    1. Advertising

  3. benben Guest

    wrote:
    > what is the easy way to check if the given string is substring of other
    > string only using c++?
    >


    s1.find(s2, 0);

    Ben
     
    benben, Nov 13, 2006
    #3
  4. VJ Guest

    VJ, Nov 13, 2006
    #4
  5. benben Guest

    VJ wrote:
    > wrote:
    >> what is the easy way to check if the given string is substring of other
    >> string only using c++?
    >>

    >
    > You can use substr function:


    Are you serious?
     
    benben, Nov 13, 2006
    #5
  6. VJ Guest

    benben wrote:
    > VJ wrote:
    >
    >> wrote:
    >>
    >>> what is the easy way to check if the given string is substring of other
    >>> string only using c++?
    >>>

    >>
    >> You can use substr function:

    >
    >
    > Are you serious?


    Was serious until I read his question again
     
    VJ, Nov 13, 2006
    #6
  7. Guest

    wrote:
    > std::string s("Some text is here");
    > std::string::size_type pos = s.find("text");
    > if (pos == std::string::npos)
    > {
    > // Not found
    > }
    > else
    > {
    > // Substring is on index pos
    > }




    Hi, Thanks for this code, But i tried this before asking question and
    found that it is not working.
    what is wrong in following code?
    //cFwdTaskList=allstream_LNP_install,allstream_LNP_disconnect,allstream_native_install,allstream_native_disconnect,allstream_change_TN,allstream_change_411,allstream_change_BNS,allstream_change_411_911_PICCare_BNS,allstream_move,allstream_cmo_disconnect,allstream_change_PIC,allstream_add_alternate_TN,allstream_remove_alternate_TN,allstream_LNP_reinstate,allstream_remove_alternate_TN,allstream_alternate_change_411,allstream_alternate_change_BNS,allstream_alternate_cmo_disconnect


    //functionNm=allstream_move

    bool SoapCallProtocol::isPresentInFwdList(RWCString functionNm)
    {
    TRACE (4, "in isPresentInFwdList");

    std::string s(cFwdTaskList);
    std::string::size_type pos = s.find(functionNm);
    if (pos == std::string::npos)
    {
    TRACE (4, "You are executing the function :" << functionNm);
    TRACE (4, "function :" << functionNm<<"is not present in fwd
    list.");
    return false;
    }
    else
    {
    TRACE (4, "You are executing the function :" << functionNm);
    TRACE (4, "function :" << functionNm<<"is present in fwd list.");
    return true;
    }

    TRACE (4, "By default returning false.");
    return false;
    }
     
    , Nov 14, 2006
    #7
  8. ondra.holub Guest

    Hi.

    I do not know the type RWCString (argument of function). If it is
    something, what may be typecasted to std::string, there shouldn't be
    any problem. I tried it with this example:
    #include <string>
    #include <iostream>

    bool isPresentInFwdList(const std::string& functionNm)
    {
    std::cerr << "in isPresentInFwdList";

    std::string s("item1|item2|item3");
    std::string::size_type pos = s.find(functionNm);
    if (pos == std::string::npos)
    {
    std::cerr << "You are executing the function: " << functionNm;
    std::cerr << " function: " << functionNm << " is not present in
    fwd list.\n";
    return false;
    }
    else
    {
    std::cerr << "You are executing the function: " << functionNm;
    std::cerr << " function: " << functionNm << " is present in fwd
    list.\n";
    return true;
    }
    }

    int main()
    {
    std::cout << isPresentInFwdList("item2") << "\n";
    std::cout << isPresentInFwdList("item8") << "\n";
    }

    and it worked.

    I suggest to write in trace also value of function parameter. I think
    the problem is there.
     
    ondra.holub, Nov 14, 2006
    #8
  9. Guest

    Thanks, the problem was in cFwdTaskList.
    The code is working fine now. thanks.

    ondra.holub wrote:
    > Hi.
    >
    > I do not know the type RWCString (argument of function). If it is
    > something, what may be typecasted to std::string, there shouldn't be
    > any problem. I tried it with this example:
    > #include <string>
    > #include <iostream>
    >
    > bool isPresentInFwdList(const std::string& functionNm)
    > {
    > std::cerr << "in isPresentInFwdList";
    >
    > std::string s("item1|item2|item3");
    > std::string::size_type pos = s.find(functionNm);
    > if (pos == std::string::npos)
    > {
    > std::cerr << "You are executing the function: " << functionNm;
    > std::cerr << " function: " << functionNm << " is not present in
    > fwd list.\n";
    > return false;
    > }
    > else
    > {
    > std::cerr << "You are executing the function: " << functionNm;
    > std::cerr << " function: " << functionNm << " is present in fwd
    > list.\n";
    > return true;
    > }
    > }
    >
    > int main()
    > {
    > std::cout << isPresentInFwdList("item2") << "\n";
    > std::cout << isPresentInFwdList("item8") << "\n";
    > }
    >
    > and it worked.
    >
    > I suggest to write in trace also value of function parameter. I think
    > the problem is there.
     
    , Nov 14, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. juli
    Replies:
    3
    Views:
    8,881
    Shiva
    Dec 6, 2004
  2. Lord0
    Replies:
    1
    Views:
    581
    Thomas Weidenfeller
    Apr 19, 2006
  3. chiara
    Replies:
    6
    Views:
    490
    Barry Schwarz
    Oct 6, 2005
  4. 2Barter.net
    Replies:
    0
    Views:
    380
    2Barter.net
    Dec 13, 2006
  5. Replies:
    3
    Views:
    213
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page