CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2005
    Posts
    7

    Question 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

  2. #2
    Join Date
    Sep 1999
    Posts
    137

    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.

  3. #3
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Finding a word in CString?

    Quote 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:
    Quote 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?

  4. #4
    Join Date
    May 2005
    Posts
    7

    Re: Finding a word in CString?

    Quote 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

  5. #5
    Join Date
    Oct 2004
    Location
    Romania/Cluj-Napoca(Kolozsvár)
    Posts
    214

    Re: Finding a word in CString?

    Quote 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!

  6. #6
    Join Date
    May 2005
    Posts
    7

    Re: Finding a word in CString?

    Ahh, thanks much =)
    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
  •  





Click Here to Expand Forum to Full Width

Featured