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

Thread: [RESOLVED] String Find and nPos

  1. #1
    Join Date
    Nov 2009
    Posts
    6

    [RESOLVED] String Find and nPos

    Hi,

    I am using string find to find a keyword in a name (of a node). If the keyword exists, I want it to do something, otherwise ignore.

    size_t subMesh = tempName.find("subMesh");

    if (subMesh == string::nPos)
    {
    // subMesh was not found
    // but this block executes when subMesh IS found... ???
    }

    After much frustration I discovered that the code block inside the if statement occurs when subMesh IS found. That is, when "subMesh" is found in tempName, the codeblock inside the if statement executes, otherwise it does not. I am wondering why this is so and is there a fix for this?

    Thank you.

  2. #2
    Join Date
    Nov 2009
    Posts
    6

    Re: String Find and nPos

    REPOST: since I cannot edit the post, I thought I'd reply with the code tags

    Hi,

    I am using string find to find a keyword in a name (of a node). If the keyword exists, I want it to do something, otherwise ignore.

    Code:
    size_t subMesh = tempName.find("subMesh");
    
    if (subMesh == string::nPos)
    {
    // subMesh was not found 
    // but this block executes when subMesh IS found... ???
    }
    After much frustration I discovered that the code block inside the if statement occurs when subMesh IS found. That is, when "subMesh" is found in tempName, the codeblock inside the if statement executes, otherwise it does not. I am wondering why this is so and is there a fix for this?

    Thank you.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: String Find and nPos

    I suggest that you post the smallest and simplest compilable program that demonstrates the problem. Fix the subject string as well so that we can easily verify the output.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Nov 2009
    Posts
    6

    Re: String Find and nPos

    Excellent suggestion

    I made a small program and it worked which pointed out the error in my current program.

    Thanks!

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)