Left, Right, Mid?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What are the equivalant to left, right, etc. in C#. How can I extract
strings from a string without them? For instance, I have a string like so:

"c:\thedir\subfolder\maybeanother\__TFMF_lrniez55ufziugv2nnznzeyt_b9c9c1d5-230a-43e1-b38a-7a44_0___Selected.eps"

I need to return just the directory name "c:\thedir\subfolder\maybeanother\"
which may be different, and the file name minus the extension and the
"___Selected" portion, so return
"__TFMF_lrniez55ufziugv2nnznzeyt_b9c9c1d5-230a-43e1-b38a-7a44_0". I could
do this in VB.NET as well as TSQL but how in C#? Thank you.
 
When dealing with path names use the System.IO.Path object.

So to get the directory name of some path, like the one in your example,
just do...

string dir = System.IO.Path.GetDirectoryName(somePath);

File name minus extension...

string fn = System.IO.Path.GetFileNameWithoutExtension(somePath);

Check out the other methods in Path. They are very useful in dealing with
paths.

But, to answer your question as to the equivalent of Left, Right, and
Mid...see string.SubString which can be used in combination with
string.IndexOf, string.LastIndexOf, etc. string.Split may also be useful to
you.

-Drew
 
Thanks a lot, I've managed to extract path info from the string and even the
remaining file info, however, I cannot seem to remove the last
___Selected.ext part from the file. The ext may be .eps or .jpeg, can vary.
In sql I'd get charindex of '___' and trim from there but I cant seem to do
it do it in c#. Any thoughts?
 
Someting like:
string foo = "@__TFMF_lrniez55ufziugv2nnznzeyt_b9c9c1d5-­
230a-43e1-b38a-7a44_0___Selected.eps";
string bar = foo.Substring(0, foo.LastIndexOf("___"));

Marc
 
Here is one way to solve the problem (does not answer the question, but
solves the problem).

string file =
"c:\thedir\subfolder\maybeanother\__TFMF_lrniez55ufziugv2nnznzeyt_b9c9c1d5-230a-43e1-b38a-7a44_0___Selected.eps";

string[] fileSplit = file.Split("\".ToCharArray());
StringBuilder filePathBuilder = new StringBuilder();

for(int i=0;i<fileSplit.Length;i++)
{
if(!fileSplit.Contains("_")
{
if(i!=0)
filePathBuilder.Append("\");
filePathBuilder.Append(fileSplit;
}
}

string filePath = filePathBuilder.ToString();

Regex is another possibility, which is probably cleaner in the long run. I
would also note that most of the file handling bits in .NET will allow you
to separate a path from a file. In fact, one possibility is to get the file
path and the file name and remove the later from the former. Pseudocoded:

File file = File.GetFile(path);
string fullFileName = File.FullName;
string fileName = File.Name;

string filePath = fullFileName.Replace(fileName, String.Empty);

The syntax above is from memory and very likely to be wrong. The concept is
sound however. I would also examine if the File or FileInfo class has the
path only, as that would be even easier.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top