string.find()

This is a discussion on string.find() within the C++ Programming forums, part of the General Programming Boards category; I thinking of something when using the find() function for a string. In the code below I am serching for ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    59

    string.find()

    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.

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

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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
    Posts
    59
    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-18-2006, 11:12 PM
  2. help with 'string.find'
    By barneygumble742 in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2005, 11:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21