How to get the last part of a URL after the last slash (/) in C#

3 Answers

0 votes
using System;

class Program
{
    static void Main() {
        string url = "http://www.website.com/abc/xyz";
        
        int pos = url.LastIndexOf("/") + 1;
        
        string s = url.Substring(pos, url.Length - pos);

        Console.WriteLine(s); 
    }
}



/*
run:

xyz

*/

 



answered Feb 20, 2020 by avibootz
0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        string url = "http://www.website.com/abc/xyz";

        string s = url.Split('/').Last();

        Console.WriteLine(s); 
    }
}



/*
run:

xyz

*/

 



answered Feb 20, 2020 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        string url = "http://www.website.com/abc/xyz";

        string s = url.Substring(url.LastIndexOf("/") + 1);

        Console.WriteLine(s); 
    }
}



/*
run:

xyz

*/

 



answered Feb 20, 2020 by avibootz

Related questions

1 answer 188 views
1 answer 222 views
2 answers 312 views
2 answers 250 views
1 answer 215 views
1 answer 196 views
3 answers 345 views
...