CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: CString Find

  1. #1
    Guest

    CString Find

    I was hoping for some clarification on the CString Find() function. The help that I have states that if you call the function with a second param (int nStart), that character will be excluded from the find. Thus the following code should set i to 5, 11, -1 on the respective steps.

    int i =0;
    CString Junk = "Hello World Web";
    i = Junk.Find(" ", i);
    i = Junk.Find(" ", i);
    i = Junk.Find(" ", i);

    I found that it always returns 5. However, if change to Find(" ", i+1) I get the right values. My guess is that the help is a typo. Have I missed something on this? Maybe a mention of this help error in the String section on CG would be nice.

    Thanks
    R Pellegrini




  2. #2

    Re: CString Find

    The CString class does not have a Find() method that takes two parameters, so I am confused. What version of MFC (or Visual C++) are you using??

    LA Leonard

    Definitive Solutions is the maker of AprCalc, the premier shareware mortgage calculator.

  3. #3
    Join Date
    May 1999
    Location
    Atlanta, GA, USA
    Posts
    443

    Re: Quite Normal?

    Hi.

    Check this MSDN help.

    // Second example demonstrating
    // CString::Find( TCHAR ch, int nStart )

    CString str("The stars are aligned");
    n = str.Find('e', 5);
    ASSERT(n == 12);

    Hope for help.
    -Masaaki Onishi-



  4. #4
    Guest

    Re: CString Find

    I am using VC6.0. The help that I was accessing was the MSDN that came with it. The first part of the help starts out:

    CString::Find
    int Find( TCHAR ch ) const;
    int Find( LPCTSTR lpszSub ) const;
    int Find( TCHAR ch, int nStart ) const;
    int Find( LPCTSTR pstr, int nStart ) const;

    and later states:

    nStart: The index of the character in the string to begin the search with, or 0 to start from the beginning. The character at nStart is excluded from the search if nStart is not equal to 0.

    So my point is that the find routine should not return 5 for the following code, but it does.

    CString temp = "Hello there world";
    return temp.Find(" ", 5);




  5. #5
    Guest

    Re: Quite Normal?

    Thanks, but the problem comes when the character at nStart == the target character. To simulate, you can change the example to:

    CString str("The stars are aligned");
    n = str.Find('e', 2);
    ASSERT(n == 12);

    The ASSERT will fail even though the help states that it shouldn't.


  6. #6
    Join Date
    May 1999
    Posts
    57

    Re: Quite Normal?

    It starts at 1.

    0 = start of string
    1 = miss out the first char and start at the second.

    so by using 2 in your example, your missing out the 'h' and starting at the e.


  7. #7
    Guest

    Re: CString Find

    My question is: Why did the MFC guys add "The character at nStart is excluded
    from the search if nStart is not equal 0"?? This is just overkill that is
    unintuitive, inconsistent, and would make programming a custom search using the
    CString::Find() very error prone.

    Using the definition in the help for CString::Find(s, nStart) how would you
    specify that you want to start searching from s[1] and you want to
    include s[1] in the search? If I request a search to start at position
    nStart, I expect that position nStart is part of the search. Why should only
    position 0 hold to common sense?

    I believe the guys at Microsoft saw how dumb it sounded to not include
    the start position for all cases and implemented the search correctly (not in
    time to change the online-help, though). You can always look at the
    CString::Find source code to see what is actually happening.

    Regards,

    Paul


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured