user3884462
user3884462

Reputation: 201

How get a substring after the 3rd occurrence of a string c#

How can I find use the substring method so I can get everything after the 3rd occurence of "/" for example I have string that contains: http://TEST.COM/page/subpage

How can I extract page/subpage this from the above string (in c#)?

Upvotes: 0

Views: 1352

Answers (3)

Shaikh Farooque
Shaikh Farooque

Reputation: 2630

There could be various ways:

As @Selman mentioned using Uri class:

var url = new Uri("http://TEST.COM/");
var path = url.MakeRelativeUri(new Uri("http://TEST.COM/page/subpage"));

using IndexOf

var offset = myString.IndexOf('/');
offset = myString.IndexOf('/', offset+1);
var result = myString.IndexOf('/', offset+1);

Upvotes: 0

Sowiarz
Sowiarz

Reputation: 1081

You can use split() :

    // The directory 
    string dir = "http://TEST.COM/page/subpage";
    // Split on directory separator
    string[] parts = dir.Split('/');

And you will have an array. You can do with it what you want. And Split() string as you wish.

Upvotes: 1

Selman Genç
Selman Genç

Reputation: 101681

If you are working with URL's you can use Uri class:

var url = new Uri("http://TEST.COM/");
var path = url.MakeRelativeUri(new Uri("http://TEST.COM/page/subpage"));

Upvotes: 2

Related Questions