CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Posts
    121

    Extracting from strring

    Help!

    I hope someone can give me some hint on how to do this.

    I have a variable

    path = "C:\john\1999.01.01(Monday)\15h 35m 54s (Virtual Recorder)"

    I need to isolate "Virtual Recorder" into a separate variable. Although Virtual Recorder could have been "LAVAC" or "BANANA". The key is the parentheses.

    Thank you in advance


  2. #2
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: Extracting from string

    First find the last backslash, then look for the parens from that point.
    strrchr can do this and, if path is a CString, it probably has some
    members that can also.

    If path is just a character buffer, once you have the last backslash
    calling strtok with a delimiter string of "()" will do it. Remember
    that strtok is destructive : it inserts nulls, so if you need to retain
    the original string use strtok on a copy of it.

    I don't want to just hand you the code because you would learn much
    more by wrestling with this kind of string parsing yourself.



  3. #3

    Re: Extracting from strring

    Given s is a CString = "C:\john\1999.01.01(Monday)\15h 35m 54s (Virtual Recorder)".

    int nOpenParen(s.ReverseFind('('));

    if (-1 != nOpenParen) {
    int nCloseParen(s.Mid(nOpenParen).Find(')'));

    if (-1 != nCloseParen) {
    return s.Mid(nOpenParen + 1,
    nCloseParen - nOpenParen);
    }
    }

    I *think* the above is right... step through it and watch out for "off-by-one" errors, though.



    LA Leonard - Definitive Solutions, Inc.

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