-3

how can I get a value of the string after last'/'

string path=http://localhost:26952/Images/Users/John.jpg

I would like to have as a result something like : John.jpg

ruud
  • 210
  • 4
  • 12
  • By finding the LastIndexOf() the / – Sami Kuhmonen Jan 05 '16 at 15:46
  • Possible duplicate of [C# third index of a character in a string](http://stackoverflow.com/questions/4578735/c-sharp-third-index-of-a-character-in-a-string) – haddow64 Jan 05 '16 at 15:46
  • If you always want only the part after the last slash, you can use [String.LastIndexOf](https://msdn.microsoft.com/en-us/library/system.string.lastindexof%28v=vs.110%29.aspx). – adv12 Jan 05 '16 at 15:47
  • 2
    `LastIndexOf()`, and yeah this is probably a duplicate of 100 questions. – Drew Kennedy Jan 05 '16 at 15:47
  • If you are looking at file names specifically, then you can use the Sysem.IO library. Call `var fileNameOnly = Path.GetFileName(path);` – Nathan C Jan 05 '16 at 15:48
  • Are you mainly looking for how to manipulate strings in general, or how to work with paths and file names? For string manipulation, look at the MSDN for String to see all the methods available. For path manipulation, look at the MSDN for System.IO.Path. – Nathan C Jan 05 '16 at 15:50

2 Answers2

12

I think using Path.GetFileName method is a better way instead of string manipulation.

string path = "http://localhost:26952/Images/Users/John.jpg";
var result = Path.GetFileName(path);
Console.WriteLine(result); // John.jpg
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
2

Split by '/' and get the last part:

 var url = "http://localhost:26952/Images/Users/John.jpg";
 var imageName = url.Split('/').Last();
GvS
  • 52,015
  • 16
  • 101
  • 139