I have strings that have a directory in the following format:
C://hello//world
How would I extract everything after the last /
character (world
)?
To get the value of a string after the last slash, call the substring() method, passing it the index, after the last index of a / character as a parameter. The substring method returns a new string, containing the specified part of the original string. Copied!
Use the . substring() method to get the access the string 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.
using System.Linq;
var s = "C://hello//world"; var last = s.Split('/').Last();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With