Ronaldinho Learn Coding
Ronaldinho Learn Coding

Reputation: 13824

Get current URL but excluding some parts

The current URL looks like: http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA

I need to get the part before the #, so it's gonna be: http://mycompany.com/jobs/java-developer.aspx

How can I do that in code behind?

Upvotes: 0

Views: 127

Answers (4)

Adam
Adam

Reputation: 2440

    private String getGoodString(String url)
    {
         return url.Substring(0, url.IndexOf("#") - 1);
    }

This will return everything up to but not including the # symbol in your string. Assuming you have a # to signal when to stop getting the string of the URL. Otherwise if there are multiple #. You can use lastIndexOf, or firstIndexOf if you need to "trim the fat".

Upvotes: 0

Mehmet Eren Yener
Mehmet Eren Yener

Reputation: 2036

Uri myUri = new Uri("http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA");
string url = myUri.Host + myUri.LocalPath;

you can create uri with string then get the parts

Upvotes: 0

Konrad Kokosa
Konrad Kokosa

Reputation: 16898

You can use Uri class:

var uri = new Uri("http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA");
var result = uri.GetLeftPart(UriPartial.Path);

Upvotes: 1

JasonS
JasonS

Reputation: 199

string url = "http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA";
string path = url.Substring(0, url.IndexOf("#"));

Upvotes: 0

Related Questions