Click to See Complete Forum and Search --> : CString Find
April 7th, 1999, 03:49 PM
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
LALeonard
April 7th, 1999, 04:49 PM
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.
Masaaki
April 7th, 1999, 05:05 PM
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-
April 7th, 1999, 05:45 PM
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);
April 7th, 1999, 05:49 PM
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.
Wilka
April 7th, 1999, 08:50 PM
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.
April 8th, 1999, 12:34 AM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.