Thread: String member function find error

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    String member function find error

    Why does it keep returning string::npos everytime i use:
    Code:
    size_type find(
       const value_type* _Ptr, 
       size_type _Off = 0,
       size_type _Coun
    like this:

    Code:
    string str;
    str = "find this in the string";
    
    size_t pos = str.find("this", 4, 10);
    if(pos == string::npos)
        return 0;
    //pos always == npos?
    //Why? Am I doing something wrong?
    My Website
    010000110010101100101011
    Add Color To Your Code!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The third argument to find() is the number of characters to find. You can omit that and use the 2 argument version, since find() will be able to figure out that "this" has four characters. Or you can change 10 to 4 in your function call.

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    try
    Code:
    int pos = str.find("this",0/*starting at 0 index*/ );

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    well i have a program managing huge strings, and i have a function that looks for a keyword between 2 positions.

    What i really want it to do is to find "keyword" between position 82 and 120 for example. How can i limit the search between those two positions?
    My Website
    010000110010101100101011
    Add Color To Your Code!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You might have to use substr to get a substring and then use find on that.

  6. #6
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Thank you. I got it working.
    My Website
    010000110010101100101011
    Add Color To Your Code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  2. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM