Thread: String Help

  1. #1
    imhungry
    Guest

    String Help

    I recently had to write a program where it should perform a certain action only when there was a letter 'a' in the string. I ended up using something like:
    Code:
    if (b.find('A')!=npos)
       	return b;
    but my question is, is there a more efficient way to find out if a string contains a certain character? I would have thought that if there is npos there should be something like pos so you could do:
    Code:
    if (b.find('A') == pos)
            return b;
    Any help is greatly appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    As far as I can see there is no difference in efficiency between the code you wrote and the code you wanted to write, but there is no "pos", npos is generally defined as -1, and find returns -1 if it can't find the character.

    If you dont need the function to return the string you could simply write:

    return b.find('A') != npos;

    that would return true or false.. but depending on your situation you may actually be intending to return b.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM