0

I want to Split the url into two part using RegEx. I have saved the xml response into datatable and iterate through the each row using foreach. When I use the datatable value for regEx only 0 will be available. When I use the array index 1 it give the following Exception.

System.IndexOutOfRangeException: Index was outside the bounds of the array.

The Following is gives and Exception.

foreach (DataRow row in ndt.Rows)
{
    string imgurl =row["image1"].ToString();

    String[] fimgurl = Regex.Split(imgurl, @"small/");
    String  simgurl = fimgurl[1];
}

The Following It's working without any issue.

foreach (DataRow row in ndt.Rows)
{
    //TextBox1.Text = row["ImagePath"].ToString();
    string imgurl ="http://www.hotelbeds.com/giata/small/12/124356/124356a_hb_w_001.jpg";
    String[] fimgurl = Regex.Split(imgurl, @"small/");
    String  simgurl = fimgurl[1];
}

I Have saved the same url as string in My Datatable. I couldn't find what's wrong with that. Can anyone please help me on this?

1
  • Well imgurl obviously doesn't equal http://www.hotelbeds.com/giata/small/12/124356/124356a_hb_w_001.jpg?? Commented Jul 19, 2013 at 8:44

5 Answers 5

5

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. --Jamie Zawinski

var uri = new Uri("http://www.hotelbeds.com/giata/small/12/124356/124356a_hb_w_001.jpg");

var firstPart = uri.Host + String.Concat(uri.Segments.Take(3));
  // returns: "www.hotelbeds.com/giata/small/"

var lastPart = String.Concat(uri.Segments.Skip(3));
  // returns: "12/124356/124356a_hb_w_001.jpg"

If you need the http, you can use uri.Scheme. Obviously just change 3 to some other number if you want to split in a different place.

Additionally you can search uri.Segments for the index of which directory you want if it changes.

Sign up to request clarification or add additional context in comments.

Comments

1

It can means only that

string imgurl =row["image1"].ToString();

not equals value, that you provides. Try to debug your code and find out what exactly value has your variable imgurl

Comments

0

I don't think there is anything wrong with your code.

The problem is that you have an url that don't contain "small/" in it. so the Split function return an array of length 1, so when you try to access the index 1 you have exception.

If you want more detail you have to post the content of ndt.

a simple way to debuf is to print the url to the console so you can see the url:

foreach (DataRow row in ndt.Rows)
            {
    string imgurl =row["image1"].ToString();
    Console.WriteLine(imgurl)
    String[] fimgurl = Regex.Split(imgurl, @"small/");
    String  simgurl = fimgurl[1];
}

3 Comments

there is no console in ASP.Net
use System.Diagnostics.Debug.WriteLine instead
That also won't work in Asp.Net. This is HTTP, your thinking like a windows app. The only place you can write to is the HTTP response.
0

in your grid there may be images without small/ in the url so better to add condition like below

if(fimgurl.Length >1)
      String  simgurl = fimgurl[1];

Comments

0

I think you have an error in column name:
in first variant you use image1 column, in second variant you use ImagePath (in comment), maybe you must use ImagePath column like:

string imgurl = row["ImagePath"].ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.