Function to search for Characters

T

thiagomrd

So.. I am new in C++.. just wanted to know if someone can help me doind
a function to check if there is a character in a string.. for example

string ex
ex << hello 33

if(there is 33 in strin ex)
- do wateva -

if(there is hello in string ex)
- do wateva-

if(!there is A in string ex)
- do wateva -

Thanks in advance,
Thiago
 
B

Bob Hairgrove

So.. I am new in C++.. just wanted to know if someone can help me doind
a function to check if there is a character in a string.. for example

string ex
ex << hello 33

if(there is 33 in strin ex)
- do wateva -

if(there is hello in string ex)
- do wateva-

if(!there is A in string ex)
- do wateva -

Thanks in advance,
Thiago

std::string.find() is your friend.

FAQ: http://www.parashift.com/c++-faq-lite/
 
R

Rolf Magnus

So.. I am new in C++.. just wanted to know if someone can help me doind
a function to check if there is a character in a string.. for example

string ex
ex << hello 33

if(there is 33 in strin ex)
- do wateva -

if(there is hello in string ex)
- do wateva-

if(!there is A in string ex)
- do wateva -

#include <string>
#include <iostream>

int main()
{
std::string ex("hello 33");

if (ex.find("33") != std::string::npos)
std::cout << "There is 33 in the string\n";

if (ex.find("hello") != std::string::npos)
std::cout << "There is hello in the string\n";

if (ex.find("A") == std::string::npos)
std::cout << "There is no A in the string\n";
}
 
D

David Harmon

On Tue, 17 Jan 2006 13:09:27 +0100 in comp.lang.c++, Rolf Magnus
int main()
{
std::string ex("hello 33");

if (ex.find("33") != std::string::npos)
std::cout << "There is 33 in the string\n";

You get an "A"
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top