Ian Lundberg
Ian Lundberg

Reputation: 1883

How to extract substring from this string?

I am trying to extract the string at the end of a URL. Example:

C:\this\that\ExtractThisString.exe
             ^^^^^^^^^^^^^^^^^^^^^

I am trying to get the ExtractThisString.exe from that string, however it has an unknown amount of \'s. I want it to basically grab the URL and list whatever is at the end of it.

Upvotes: 3

Views: 8101

Answers (6)

Alfred Wallace
Alfred Wallace

Reputation: 23

I find and use this elegant way using System.IO

string file1 = Path.GetFileName(@"C:\this\that\ExtractThisString.exe");

or if you want without extension

string file2 = Path.GetFileNameWithoutExtension(@"C:\this\that\ExtractThisString.exe");

or only the extension

string ext = Path.GetExtension(@"C:\this\that\ExtractThisString.exe");

Upvotes: 0

Steve
Steve

Reputation: 216353

To find the last occurence of a specified character use

int pos = yourString.LastIndexOf(@"\");

then extract the substring

string lastPart = yourString.Substring(pos+1);

EDIT I am reviewing this answer after 15 months because I have really missed a key point in the question. The OP is trying to extract a filename, not just simply finding the last occurrence of a given character. So, while my answer is technically correct it is not the best one because the NET framework has a specialized class to handle file name and paths. This class is called Path and you can find a simple and very effective way to achieve your result using Path.GetFileName as explained in the answer from @Adriano.

I wish also to highlight the fact that using the methods from the Path class you have the advantage of code portability because the class handles the situation when a different operating system uses a different directory separator char.

Upvotes: 3

buck
buck

Reputation: 205

string path = @"c:\this\that\extractthisstring.exe";
Console.WriteLine(path.Split('\\').Reverse().First());

Upvotes: 1

Adriano Repetti
Adriano Repetti

Reputation: 67148

Use helper methods of System.IO.Path class. In your case:

string fileName = Path.GetFileName(@"C:\this\that\ExtractThisString.exe");

Just for fun, if you have to make it by yourself you should start searching for the index of the last Path.DirectorySeparatorChar. If that's not the last character in the string then you can use String.Substring to extract all text after that index.

Upvotes: 9

Hupperware
Hupperware

Reputation: 979

Do it once for everything...

public static string FileAndExtension(this string aFilePath) {
 return aFilePath.Substring(aFilePath.LastIndexOf("\\") + 1);
}

"C:\\this\\that\\ExtractThisString.exe".FileAndExtension()

OR

public static string EverythingAfterLast(this string aString, string aSeperator) {
 return aString.Substring(aString.LastIndexOf(aSeperator) + 1);
}

"C:\\this\\that\\ExtractThisString.exe".EverythingAfterLast("\\")

Upvotes: 1

hermiod
hermiod

Reputation: 1168

Try this

var str = @"C:\this\that\ExtractThisString.exe";
var filename = str.Substring(str.LastIndexOf("\\")+1);

Upvotes: 2

Related Questions