Regex class

  • Thread starter Thread starter _nabuchodonozor
  • Start date Start date
N

_nabuchodonozor

Hi
I have string as path\track.mp3 eg "C:\Mp3\Korn\korn - trash.mp3" and
I want to have only korn - trash.mp3 file without path. How should I
do it?
 
I have string as path\track.mp3 eg "C:\Mp3\Korn\korn - trash.mp3" and
I want to have only korn - trash.mp3 file without path. How should I
do it?

Use Path.GetFilename

Jon
 
_nabuchodonozor said:
I have string as path\track.mp3 eg "C:\Mp3\Korn\korn - trash.mp3" and
I want to have only korn - trash.mp3 file without path. How should I
do it?

string s = @"C:\Mp3\Korn\korn - trash.mp3";
Console.WriteLine(Regex.Match(s, @"[^\\]+$").Value);
 
_nabuchodonozor said:
I have string as path\track.mp3 eg "C:\Mp3\Korn\korn - trash.mp3" and
I want to have only korn - trash.mp3 file without path. How should I
do it?

string s = @"C:\Mp3\Korn\korn - trash.mp3";
Console.WriteLine(Regex.Match(s, @"[^\\]+$").Value);

That certainly copes with the example given - but it won't cope with
the perfectly valid path "C:/Mp3/Korn/korn - trash.mp3".

Given that .NET provides a class specifically for this purpose (Path)
I think it makes sense to use it :)

Jon
 
Given that .NET provides a class specifically for this purpose (Path)
I think it makes sense to use it :)

Jon

Use the static class:
System.IO.Path

or use the instance class:
System.IO.FileInfo

Which you pick depends if you will do more operations with it or not.
If youre going to rename the file, or get propeties on it, use FileInfo
 

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