473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Nov 13 '06 #1
8 19730
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
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
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ivan Demkovitch | last post by:
Hi! I would like to check if string contain specific substring. How to do it? P.S. I've done something stupid like String.Replace
3
by: Bill | last post by:
I just ran into a situation where string data from a mainframe contained a couple of non-alphanumeric characters (hex CC and C8). I was parsing a field that occurred after these unexpected...
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
10
by: kd | last post by:
Hi All, Is there a command to check whether a given string is present in a text file, without having to read the contents of the file, a line at a time and then check for the given string? ...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
2
by: NEMA | last post by:
How can i check the String letters? i have a textbox and will filled with numbers. i want to check if the number contain a '0' in font of the numbers, e.g. '090',i would like to cut the 1st '0',...
10
by: mandyj | last post by:
Given String:- ST200STT:35697600114106009,20070407,08:09:00,+18.511246,+73.842756,1.430,332.87,1,0000,0 I wanna separate this string in the format below,pls help me out... 35697600114106009 =...
13
by: abcmavdi | last post by:
i want to replace a word in given string e. g string ---- He is a good man. in above string i want to replace "good" word with "gentle" so finally i want to print bellow string :-
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.