Click here to Skip to main content
14,297,239 members
Rate this:
Please Sign up or sign in to 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 3 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use string.LastIndexOf and string.Substring:
string lastBit = input.Substring(input.LastIndexOf('/'));
   
Comments
Maciej Los 3 days ago
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Another way is to use Uri class[^]:
Uri uri = new Uri(" https://www.codeproject.com/Questions/5225378");
string lastSegment = uri.Segments.Last();


or String.Split[^] method.
string last = input.Split('/').Last();
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100