Extract certain test from a string

  • Thread starter Thread starter Todd
  • Start date Start date
T

Todd

Hello

I've got a string in the format of string z = "123456 (D:\\) 789"

I'm interesting in extracting just "D:\" but I'm not sure how do that. I
can't just go by the position because that may change.

Thanks
 
I've got a string in the format of string z = "123456 (D:\\) 789"

I'm interesting in extracting just "D:\" but I'm not sure how do that. I
can't just go by the position because that may change.

More details, please. What is the GENERALIZED pattern that you want to
match? Because if it's always exactly D:\ then String.Contains() should
suffice.
 
I've got a string in the format of string z = "123456 (D:\\) 789"

I'm interesting in extracting just "D:\" but I'm not sure how do that.  I
can't just go by the position because that may change.

If it's not defined by position, then what is is defined?

Unless you outline the specific rules according to which the desired
substring should be identified, there's no way to say anything on the
matter.
 
The string that I have varies in length.. It is a string of a volume label
and the drive letter is also in there. I don't have any control of the
format of the string that is being returned to me, but I just want to get
the drive letter, and since the length of the volume label will vary I can't
get go to that position, but I know the drive letter is within ( ).

I was wondering id there is a quick way to grab the data between ( and ) so
if I do have a string z = "123456 (D:\\) 789" I want to grab just d:\
 
Todd said:
I was wondering id there is a quick way to grab the data between ( and ) so
if I do have a string z = "123456 (D:\\) 789" I want to grab just d:\

Quick and dirty: Split the string at "(" and ")" and read its second part.

string n = "123456 (D:\\) 789";
string[] nparts = n.Split(new char[] { '(', ')' });
string drive = nparts[1];

But be careful: If the first part, too, contains one or more split
chars, you're f*ed ;-)
 
Todd,

As I now understand you right, then you simply ask.

How to extract the drive letter from a path.

No, that's not what he's trying to do at all. There is an example
string input in his original post - does it look like a path to you?
 
The string that I have varies in length..  It is a string of a volume label
and the drive letter is also in there.  I don't have any control of the
format of the string that is being returned to me, but I just want to get
the drive letter, and since the length of the volume label will vary I can't
get go to that position, but I know the drive letter is within ( ).

I was wondering id there is a quick way to grab the data between ( and ) so
if I do have a string z = "123456 (D:\\) 789"  I want to grab just d:\

Use String.IndexOf to find the positions of "(" and ")" in the string,
and then Substring to get what's between them.

The catch here is that (and I make assumptions there, since you still
haven't explained the properties of the string - i.e. what other kinds
of characters are there apart from the drive letter), you seem to have
a number of arbitrary chars both before and after the bracketed drive
letter. I wonder if those chars can also contain "(" or ")". I.e., is
this a valid input?

"a(b)c (D:\) d(e)f"

or even:

"()( (D:\) )()"

If it is, then there's no good way to definitely extract the right
string.
 
Michaela,

Pavel pointed me on the fact that I probably understood you wrong.
But as I read it again, then this kind of questions are mostly done using
the string split
http://msdn.microsoft.com/en-us/library/b873y76a.aspx

See that it has more overloads.

Cor


Michaela Julia said:
Todd said:
I was wondering id there is a quick way to grab the data between ( and )
so if I do have a string z = "123456 (D:\\) 789" I want to grab just d:\

Quick and dirty: Split the string at "(" and ")" and read its second part.

string n = "123456 (D:\\) 789";
string[] nparts = n.Split(new char[] { '(', ')' });
string drive = nparts[1];

But be careful: If the first part, too, contains one or more split chars,
you're f*ed ;-)
 
The string that I have varies in length.. It is a string of a volume
label and the drive letter is also in there. I don't have any control of
the format of the string that is being returned to me, but I just want to
get the drive letter, and since the length of the volume label will vary I
can't get go to that position, but I know the drive letter is within ( ).

I was wondering id there is a quick way to grab the data between ( and )
so if I do have a string z = "123456 (D:\\) 789" I want to grab just d:\

So, in other words, you're looking for <any letter><colon><backslash>,
right? See how easy that was? This is what I meant when I asked you for a
generalized description of what you're looking for, and I asked for a
reason. If you can't be this exacting in words, how do you expect to be this
exacting in code? It's something you really need to work on if you want to
be a successful programmer.

A quick way would be to use a Regular Expression, but then that assumes that
you're familiar with RegEx. If you are, here's the pattern I recommend:

[a-z]:\\

Make sure you set your options for case-insensitive.
 
A quick way would be to use a Regular Expression, but then that assumes
that you're familiar with RegEx. If you are, here's the pattern I
recommend:

[a-z]:\\

I just notice where you said the drive letter will always be in parentheses,
so I amend the pattern:

\(([a-z]:\\)\)

I put a set of capturing parentheses inside the literal parentheses so you
can extract the drive letter as a group.
 
Back
Top