Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
problem
I work on asp.net core 2.2 and i need to get value of last word after last slash from url ?
as example
https://www.codeproject.com/Questions/5225378
how to get 5225378 from url ?

What I have tried:

string last = input.Split(' ').LastOrDefault();

how to get url to get last word after slash
Posted
Updated 11-Sep-19 20:06pm

Use string.LastIndexOf and string.Substring:
C#
string lastBit = input.Substring(input.LastIndexOf('/'));
 
Share this answer
 
Comments
Maciej Los 12-Sep-19 2:07am    
5ed!
Another way is to use Uri class[^]:
C#
Uri uri = new Uri(" https://www.codeproject.com/Questions/5225378");
string lastSegment = uri.Segments.Last();


or String.Split[^] method.
C#
string last = input.Split('/').Last();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900