By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

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

P: n/a
what is the easy way to check if the given string is substring of other
string only using c++?

Nov 13 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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 '06 #2

P: n/a
va************@gmail.com 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
Nov 13 '06 #3

P: n/a
VJ
va************@gmail.com 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:

http://www.cppreference.com/cppstring/substr.html
Nov 13 '06 #4

P: n/a
VJ wrote:
va************@gmail.com 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?
Nov 13 '06 #5

P: n/a
VJ
benben wrote:
VJ wrote:
>va************@gmail.com 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
Nov 13 '06 #6

P: n/a

on*********@post.cz 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_d isconnect,allstream_native_install,allstream_nativ e_disconnect,allstream_change_TN,allstream_change_ 411,allstream_change_BNS,allstream_change_411_911_ PICCare_BNS,allstream_move,allstream_cmo_disconnec t,allstream_change_PIC,allstream_add_alternate_TN, allstream_remove_alternate_TN,allstream_LNP_reinst ate,allstream_remove_alternate_TN,allstream_altern ate_change_411,allstream_alternate_change_BNS,alls tream_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 '06 #7

P: n/a
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 '06 #8

P: n/a
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 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.