Get contents after last slash
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.
Try this:
string worldWithPath = "C://hello//world";
string world = worldWithPath.Substring(worldWithPath.LastIndexOf("/") + 1);
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
.
using System.Linq;
var s = "C://hello//world";
var last = s.Split('/').Last();