Thread: string.find()

  1. #1
    Registered User
    Join Date
    Oct 2008


    I thinking of something when using the find() function for a string.

    In the code below I am serching for the first occurence of "," in the string

    What I wonder is if it is possible to either find "," or the index of the end of the string if there isnīt any "," as in this case.

    int index = 0;
    std::string str = "abcde";
    index  = str.find(",")

  2. #2
    The larch
    Join Date
    May 2006
    find returns std::string::npos if the target is not found. So you might check index and assign str.size() to it when not found (you could even wrap calls to find in your own function if you are not happy about the std::string::npos result).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2008
    I see, thanks for the info! I will try this around a bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using while with string.find()?
    By suzakugaiden in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2006, 12:12 AM
  2. help with 'string.find'
    By barneygumble742 in forum C++ Programming
    Replies: 10
    Last Post: 12-24-2005, 12:28 AM