Function to search for Characters

Discussion in 'C++' started by thiagomrd@gmail.com, Jan 17, 2006.

  1. Guest

    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
     
    , Jan 17, 2006
    #1
    1. Advertising

  2. On 17 Jan 2006 03:59:47 -0800, wrote:

    >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/

    --
    Bob Hairgrove
     
    Bob Hairgrove, Jan 17, 2006
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > 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";
    }
     
    Rolf Magnus, Jan 17, 2006
    #3
  4. David Harmon Guest

    On Tue, 17 Jan 2006 13:09:27 +0100 in comp.lang.c++, Rolf Magnus
    <> wrote,
    >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"
     
    David Harmon, Jan 18, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    765
  2. Stefan Mueller
    Replies:
    3
    Views:
    33,036
    Stefan Mueller
    Jul 23, 2006
  3. omegaman
    Replies:
    1
    Views:
    565
    omegaman
    Sep 21, 2004
  4. Micah
    Replies:
    2
    Views:
    345
    Micah
    Jun 2, 2006
  5. Abby Lee
    Replies:
    5
    Views:
    414
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page