Results 1 to 6 of 6

Thread: How to Search in a String?

  1. #1

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post How to Search in a String?

    Hello, VC++ Experts Does any of you know how to search for a String in a Specified String? Like this in VB
    VB Code:
    1. If InStr(1,the_file,"SEX WITH YOUR MAMA",vbTextCompare)<>0 Then
    2. MsgBox "Geez, you do have some good **** in this file:)"
    3. End If
    Also, I would like to know how to Pop out a MessageBox when I click on an Edit(TextBox)
    I tried this:
    PHP Code:
    switch(msg){
    case 
    WM_COMMAND:
    switch(
    LOWORD(wParam)){
    case 
    EDIT1:
    MessageBox(hwnd,"Hi","",0);
    break;
    }
    break;
    default:
    return 
    DefWindowProc(hwnd,msg,wParam,lParam);

    It pops up the MessageBox twice! How to resolve it? It doesn't happen if the EDIT1 was a Button...

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    strstr, wcsstr, _mbsstr
    Find a substring.

    char *strstr( const char *string, const char *strCharSet );

    wchar_t *wcsstr( const wchar_t *string, const wchar_t *strCharSet );

    unsigned char *_mbsstr( const unsigned char *string, const unsigned char *strCharSet );

    Routine Required Header Compatibility
    strstr <string.h> ANSI, Win 95, Win NT
    wcsstr <string.h> or <wchar.h> ANSI, Win 95, Win NT
    _mbsstr <mbstring.h> Win 95, Win NT


    For additional compatibility information, see Compatibility in the Introduction.

    Libraries

    LIBC.LIB Single thread static library, retail version
    LIBCMT.LIB Multithread static library, retail version
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version


    Return Value

    Each of these functions returns a pointer to the first occurrence of strCharSet in string, or NULL if strCharSet does not appear in string. If strCharSet points to a string of zero length, the function returns string.

    Parameters

    string

    Null-terminated string to search

    strCharSet

    Null-terminated string to search for

    Remarks

    The strstr function returns a pointer to the first occurrence of strCharSet in string. The search does not include terminating null characters. wcsstr and _mbsstr are wide-character and multibyte-character versions of strstr. The arguments and return value of wcsstr are wide-character strings; those of _mbsstr are multibyte-character strings. These three functions behave identically otherwise.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Or in english
    Code:
    /* STRSTR.C */
    
    #include <string.h>
    #include <stdio.h>
    
    char str[] =    "lazy";
    char string[] = "The quick brown dog jumps over the lazy fox";
    char fmt1[] =   "         1         2         3         4         5";
    char fmt2[] =   "12345678901234567890123456789012345678901234567890";
    
    void main( void )
    {
       char *pdest;
       int  result;
       printf( "String to be searched:\n\t%s\n", string );
       printf( "\t%s\n\t%s\n\n", fmt1, fmt2 );
       pdest = strstr( string, str );
       result = pdest - string + 1;
       if( pdest != NULL )
          printf( "%s found at position %d\n\n", str, result );
       else
          printf( "%s not found\n", str );
    }
    Output

    String to be searched:
    The quick brown dog jumps over the lazy fox
    1 2 3 4 5
    12345678901234567890123456789012345678901234567890

    lazy found at position 36
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    2) This happens over and over.
    LOWORD(wParam) in a WM_COMMAND message contains the ID!!! of the sender. Since edit boxes can send about 20 commands, you need to select the one you want. HIWORD(wparam) contains the notification code. Look for the notification code you want, like EN_SETACTIVE, that is sent when an edit control receives the keyboard focus. This is one of the messages an edt box sends when you click it. The second is probably something like CLICKED, but I couldn't find an appropriate message in the reference.
    This problem does not exist with buttons because they only receive BN_CLICKED and BN_DBLCLICKED messages.

    ADD THIS TO THE FAQ!!!!!!!!!
    I have encountered this problem 4 times in less than two months!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post I used SetWindowLong

    When I used SetWindowLong it works... But its like too long.
    But hey, how to search for a string in windows.h?
    Like I want to look for "hey" in "hey *****!"
    No matter UPPERCASE or LOWERCASE.

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    How did you use SetWindowLong???

    There is no function for searching strings in <windows.h>. You need <string.h>, or <cstring> if you use C++ and prefer this notation (it's the same). I this file is the function strstr(), which is explained in MSDN or vlatko's posts (he just copied it in). To support UNICODE, use wcsstr from <wchar.h> and to support both UNICODE and ANSI use _tcsstr from <tchar.h>.
    Usage in rather simple words: You supply two C-style strings: the first is the string to be searched, the second the string to be searched in. The function returns a pointer to the start of the search string in the searched string. You can use this pointer like it was the substring from the found string to the end of the entire string. Just don't forget that all changes you do to it will also reflect in the searched string. To get the substring from the start to the found string, this is the code:
    Code:
    // include stdlib.h and string.h
    
    char* string = "This is a very long sentence without any meaningful content, but a good example.";   // the string to be searched
    char* search = "sentence";    // the string to be searched for
    char* found = NULL;      // the return value of strstr
    char* substart = NULL;    // substring from start of string to found
    char* subend = NULL;     // an independent copy of found
    
    found = strstr(string, search);    // search the string
    if(found == NULL)
    {        // search was not found in string
    }
    subend = (char*)malloc(strlen(found));
    strcpy(subend, found);     // subend now contains a copy of found
                                   // you can safely edit this string
    
    substart = malloc((found - string) + 1);
    strncpy(substart, string, found - string);
    substart[found - string] = '\0';  // this now contains a copy of string to the point where search was found
                                // you can edit this string too
    I hope that helps, or else I've wasted a lot of time
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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