Click to See Complete Forum and Search --> : Extracting from strring


Sophie
April 8th, 1999, 02:32 PM
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

Gomez Addams
April 8th, 1999, 04:08 PM
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.

LALeonard
April 8th, 1999, 04:22 PM
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.