|
-
May 7th, 2005, 07:03 PM
#1
Finding a word in CString?
I've looked through the CString class in afx.h and can't find anything that finds a word or character.
I found these:
Code:
// find character starting at left, -1 if not found
int Find(TCHAR ch) const;
// find character starting at right
int ReverseFind(TCHAR ch) const;
// find character starting at zero-based index and going right
int Find(TCHAR ch, int nStart) const;
// find first instance of any character in passed string
int FindOneOf(LPCTSTR lpszCharSet) const;
// find first instance of substring
int Find(LPCTSTR lpszSub) const;
// find first instance of substring starting at zero-based index
int Find(LPCTSTR lpszSub, int nStart) const;
But none of these do what I'm looking for, and the always seem to return a positive number.
I'm trying to find a function that will scan a CString for a word and return true if it found it, false if it didn't. Any help would be great =D
-
May 7th, 2005, 07:37 PM
#2
Re: Finding a word in CString?
You want
int Find( LPCTSTR lpszSub ) const;
For example
Code:
CString cs( "Search me" );
CString csKey( "me" );
if (cs.Find( csKey ) == -1)
TRACE ( "NOT FOUND\n" );
else
TRACE ( "FOUND\n" );
This works because CString has defined operator LPCTSTR().
Find() looks for an LPCTSTR. Not finding one, it looks for something it can convert to an LPCTSTR.
operator LPCTSTR() means that a CString can be converted to an LPCTSTR.
-
May 7th, 2005, 07:43 PM
#3
Re: Finding a word in CString?
 Originally Posted by Kahn6
But none of these do what I'm looking for, and the always seem to return a positive number.
This one does what you are looking for:
 Originally Posted by Kahn6
// find first instance of substring
int Find(LPCTSTR lpszSub) const;
It returns -1 if the substring is not found (or the 0 based character index if it is found). Converting that into a boolean condition should be easy, or not?
-
May 8th, 2005, 02:19 AM
#4
Re: Finding a word in CString?
 Originally Posted by gstercken
It returns -1 if the substring is not found (or the 0 based character index if it is found). Converting that into a boolean condition should be easy, or not?
Sorry, I'm still a little new to MFC/API programming... Also, I don't remember indexes being mentioned in console programming =P
What is a character index?
Can't think of any witty quote or random expression to put here =/
Visual C++ v6.0
Windows XP
-
May 8th, 2005, 03:29 AM
#5
Re: Finding a word in CString?
 Originally Posted by Kahn6
What is a character index?
A string is a vector, right? A vector with characters.
Now the index is the position of elements in a vector (in this case the position of each character in a string).
In C/C++ indexes always start with 0, so the index of the first element is 0, the index of the second one 1, and so on.
Code:
// find first instance of substring
int Find(LPCTSTR lpszSub) const;
This function returns the start position of the searched string. For example, if you are searching for "pp" in "apple", it would return 1, if searching "le", would return 3, and searching "qwe", would return -1, because it is not contained in "apple".
So, if it returns a value>=0, then it's TRUE, else it's FALSE.
//Exceptions exist!
-
May 8th, 2005, 01:16 PM
#6
Re: Finding a word in CString?
Can't think of any witty quote or random expression to put here =/
Visual C++ v6.0
Windows XP
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|