Get contents after last slash

Solution 1:

string path = "C://hello//world";
int pos = path.LastIndexOf("/") + 1;
Console.WriteLine(path.Substring(pos, path.Length - pos)); // prints "world"

The LastIndexOf method performs the same as IndexOf.. but from the end of the string.

Solution 2:

using System.Linq;

var s = "C://hello//world";
var last = s.Split('/').Last();

Solution 3:

There is a static class for working with Paths called Path.

You can get the full Filename with Path.GetFileName.

or

You can get the Filename without Extension with Path.GetFileNameWithoutExtension.

Solution 4:

Try this:

string worldWithPath = "C://hello//world";
string world = worldWithPath.Substring(worldWithPath.LastIndexOf("/") + 1);