Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get contents after last slash

I have strings that have a directory in the following format:

C://hello//world 

How would I extract everything after the last / character (world)?

like image 705
john cs Avatar asked Apr 07 '13 00:04

john cs


People also ask

How to get value after last slash?

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!

How do you put a string after a slash?

Use the . substring() method to get the access the string after last slash.


2 Answers

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.

like image 94
Simon Whitehead Avatar answered Oct 01 '22 12:10

Simon Whitehead


using System.Linq;

var s = "C://hello//world"; var last = s.Split('/').Last(); 
like image 43
Matthew Steven Monkan Avatar answered Oct 01 '22 10:10

Matthew Steven Monkan