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

V

varsha.gadekar

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

ondra.holub

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
}
 
V

varsha.gadekar

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;
}
 
O

ondra.holub

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.
 
V

varsha.gadekar

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top