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

Thread: CString Find Function

  1. #1
    Join Date
    Apr 2010
    Posts
    6

    CString Find Function

    Suppose a string contains a lot of words (separated by "" , space etc.), I want to find whether a particular word exist in the string or not.

    eg. suppose I have

    CString str = L"Hai this is just for ""testing"" purpose";

    Suppose I want to search whether testing exist in str or not.(match whole word)


    What should I use??

  2. #2
    Join Date
    Apr 2008
    Posts
    133

    Re: CString Find Function

    Did you try CString::Find function?

  3. #3
    Join Date
    Apr 2010
    Posts
    6

    Re: CString Find Function

    Ya I tried, But it does not search for exact string( i.e complete match).

    For eg.:-

    CString str = L"Hai this is just for ""testingonly"" purpose";

    and I want to search whether testing exist in str or not.(match whole word) , here find() func. will return that testing exist, but in str it does not(full word)

    Please reply .

  4. #4
    Join Date
    Apr 2008
    Posts
    133

    Re: CString Find Function

    Why dont you search for "testing " since every word would have a space at the end

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,531

    Re: CString Find Function

    If it finds it, check the character before and after to see it it's space, the beginning or the end of the string.

  6. #6
    Join Date
    Apr 2010
    Posts
    6

    Re: CString Find Function

    Actually I am having this string, and want to replace OK with xyx,I dont want to change IDOK

    CString str = DEFPUSHBUTTON "OK",IDOK,179,133,50,14

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: CString Find Function

    If it finds it, check the character before and after to see it it's space, the beginning or the end of the string.
    Simple trick. Add a space on both sides and include 2 spaces in your search.

    Code:
    // instead of
    CString str = "This is a test";
    // add 2 spaces. 1 in front and 1 at the end
    CString str = " This is a test ";
    CString.Find (" is "); // add 2 spaces in search
    Now you are sure that every word is surrounded by spaces so you don't have to test anything.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,531

    Re: CString Find Function

    Quote Originally Posted by rahul.cs.rgec View Post
    Actually I am having this string, and want to replace OK with xyx,I dont want to change IDOK

    CString str = DEFPUSHBUTTON "OK",IDOK,179,133,50,14
    Then look for the quotes too.

  9. #9
    Join Date
    Apr 2010
    Posts
    6

    Re: CString Find Function

    Thanks Skizmo, for ur reply

    but Actually I am having this string, and want to replace OK with xyx,I dont want to change IDOK.
    Please don't say that find the string on the basis of "", because inside "" there can be other "".
    Actually I have to replace thousands of such strings in a loop.

    CString str = DEFPUSHBUTTON "OK",IDOK,179,133,50,14
    CString str2 = DEFPUSHBUTTON "BUTT"O"K",IDOK,179,133,50,14
    CString str3 = CONTROL "Change to",IDC_RADIO_REPLACE,"Button",BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP,15,24,48,18

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,531

    Re: CString Find Function

    Quote Originally Posted by rahul.cs.rgec View Post
    Thanks Skizmo, for ur reply

    but Actually I am having this string, and want to replace OK with xyx,I dont want to change IDOK.
    Please don't say that find the string on the basis of "", because inside "" there can be other "".
    Actually I have to replace thousands of such strings in a loop.

    CString str = DEFPUSHBUTTON "OK",IDOK,179,133,50,14
    CString str2 = DEFPUSHBUTTON "BUTT"O"K",IDOK,179,133,50,14
    CString str3 = CONTROL "Change to",IDC_RADIO_REPLACE,"Button",BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP,15,24,48,18
    What are you really trying to do?

    What's wrong with the following?
    Code:
    CString str = DEFPUSHBUTTON "OK",IDOK,179,133,50,14
    str.Replace("\"OK\"", "\"xyz\"");

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: CString Find Function

    Quote Originally Posted by rahul.cs.rgec View Post
    but Actually I am having this string, and want to replace OK with xyx,I dont want to change IDOK.
    Please don't say that find the string on the basis of "", because inside "" there can be other "".
    Actually I have to replace thousands of such strings in a loop.

    CString str = DEFPUSHBUTTON "OK",IDOK,179,133,50,14
    CString str2 = DEFPUSHBUTTON "BUTT"O"K",IDOK,179,133,50,14
    CString str3 = CONTROL "Change to",IDC_RADIO_REPLACE,"Button",BS_AUTORADIOBUTTON | BS_MULTILINE | WS_TABSTOP,15,24,48,18
    No matter which solution you choose, you'll first have to figure out the logic by yourself. People here can't help you with that - other than asking "why shouldn't this work?" -, because we don't know your requirements. Whether you implement this using CString::Find and CString::Replace or you use the Boost.Regex library, you still need to translate the logic into the appropriate code or regular expression.

    So, the first thing to do is sit down and work out the logic. What exactly needs to be replaced by what exactly. You can easily test your algorithm with most advanced text editors, which support search-and-replace based on regular expressions (Visual Studio does ).
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  12. #12
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: CString Find Function

    I do not understand your problem. Do you want to replace strings in .RC file, or you are writing some program to do that?
    If you are trying to change .RC file only - Visual Studio is powerful enough to do that.

    If you are programming, I suspect you've basic understanding of quoted string in string:
    Code:
    CString str = L"Hai this is just for \"testing\" purpose";
    Furthermore, to develop foolproof and efficient algorithm to find 'Word' you eventually have to play with characters, and not to rely on existing string functions!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)