Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > to check if the given string is substring using c++

Reply
Thread Tools

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

 
 
varsha.gadekar@gmail.com
Guest
Posts: n/a
 
      11-13-2006
what is the easy way to check if the given string is substring of other
string only using c++?

 
Reply With Quote
 
 
 
 
ondra.holub@post.cz
Guest
Posts: n/a
 
      11-13-2006
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
}

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      11-13-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
VJ
Guest
Posts: n/a
 
      11-13-2006
(E-Mail Removed) 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
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      11-13-2006
VJ wrote:
> (E-Mail Removed) 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?
 
Reply With Quote
 
VJ
Guest
Posts: n/a
 
      11-13-2006
benben wrote:
> VJ wrote:
>
>> (E-Mail Removed) 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
 
Reply With Quote
 
varsha.gadekar@gmail.com
Guest
Posts: n/a
 
      11-14-2006

(E-Mail Removed) 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;
}

 
Reply With Quote
 
ondra.holub
Guest
Posts: n/a
 
      11-14-2006
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.

 
Reply With Quote
 
varsha.gadekar@gmail.com
Guest
Posts: n/a
 
      11-14-2006
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
" Given BACK what was freely GIVEN " 2Barter.net C++ 0 12-13-2006 02:56 AM
Days in a given date range for a given month......... Lord0 Java 1 04-19-2006 04:54 PM
generate all possible strings of given length given a set of characters chiara C Programming 6 10-06-2005 01:43 AM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
find if there is a given substring inside a string juli ASP .Net 3 12-06-2004 11:52 AM



Advertisments