R00059159
R00059159

Reputation: 167

c# building strings domains

I am trying to download images. Their link may be image.png or http://www.example.com/image.png.

I made the image.png be added to the host and passed it to a list. So image.png is now http://www.example.com/image.png

But if the other type is used what I get is http://www.example.com//http://www.example.com/image.png

All I need is to get the string after the third slash. Here is some code I am tried to use:

try
{
    path = this.txtOutput.Text + @"\" + str4 + etc;
    client.DownloadFile(str, path);
}
catch(Exception e)
{

    var uri = new Uri(str);
    String host = (String) uri.Host;
    String pathToFile = "http://" + host + "/";

    int len = pathToFile.Length;

    String fin = str.Substring(len, str.Length - len);

    path = this.txtOutput.Text + @"\" + str4 + etc;
    client.DownloadFile(fin, path);
}

Upvotes: 0

Views: 108

Answers (1)

Patrick
Patrick

Reputation: 681

What are these variables all about, like str4, etc and so on? Instead of the try catch you could check wheter the string is a valid uri. Give a look here. Try to debug you code line on line and check every single variable, then you will see which line makes the mistake.

EDIT

If I understodd you right, then this would be your solution:

        string wrongResult = "example.com//http://www.example.com/image.png";
        string shouldResult = "example.com/image.png";

        int listIndexOfHttp = wrongResult.LastIndexOf("http:");
        string correctResult = wrongResult.Substring(listIndexOfHttp);

When not please describe more specific from where you get this and it is always the same structure? or alaways different?

Upvotes: 1

Related Questions