0

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?

Ronaldinho Learn Coding
  • 13,254
  • 24
  • 83
  • 110

4 Answers4

1

You can use Uri class:

var uri = new Uri("http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA");
var result = uri.GetLeftPart(UriPartial.Path);
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
0
string url = "http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA";
string path = url.Substring(0, url.IndexOf("#"));
JasonS
  • 199
  • 9
0
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

Mehmet Eren Yener
  • 2,006
  • 1
  • 23
  • 37
0
    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".

Adam
  • 2,422
  • 18
  • 29