-6

I have method

public static string UrlImageName(string name)
    {
        if (name.IndexOf("_180x140") <= 0)
        {
            var extPos = name.LastIndexOf(".");
            return name.Substring(0, extPos) + "_180x140" + name.Substring(extPos, name.Length);
        }
        return name;
    }

and view

<div class="car-img">
                <img src="@IKWelcomeScreenCellViewModel.UrlImageName(viewModel.SlideShowUrls[0])" />
            </div>

names are like this "http://www.ua/Content/images/ik-main-page/Catalog.png" and I need this http://www.ua/Content/images/ik-main-page/Catalog_180x140.png

error Index and length must refer to a location within the string

user246340
  • 45
  • 1
  • 7

5 Answers5

0

String.Substring() function takes the startingIndex as first argument and Length/number of characters tobe extracted as second argument.

Replace This:

return name.Substring(0, extPos) + "_180x140" + 
                                 name.Substring(extPos, name.Length);

With This:

return name.Substring(0, extPos.Length - extPos) + "_180x140" + 
                        name.Substring(extPos, name.Length - extPos);
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

Since the second parameter to Substring is a length, this will fail every time unless extPos is 0:

name.Substring(extPos, name.Length);

Change it to this, so that you're only selecting the portion of the string after the period.

name.Substring(extPos + 1, name.Length - extPos - 1);

Or to this, if you want to include the period in the result:

name.Substring(extPos, name.Length - extPos);

You'll probably want an extra check too, if there's a chance there won't be a period in the string.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

extPos is the position where the extension of the file starts. Now, if you want to insert that _180x140 just before the extension you could use Substring without the length. This will take the remainder of the string starting from the extPos position and you don't need any calculation to readd the extension

public static string UrlImageName(string name)
{
    if (name.IndexOf("_180x140") < 0)
    {
        var extPos = name.LastIndexOf(".");
        return name.Substring(0, extPos) + "_180x140" + name.Substring(extPos);
    }
    return name;
}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

If extPos is, let's say 5, and the string has 10 characters, then the following line will clip from index 5 to 15, which is out of bounds.

name.Substring(extPos, name.Length);

You should be doing this instead.

name.Substring(extPos, name.Length - extPos);

or, simply,

name.Substring(extPos);
dcastro
  • 66,540
  • 21
  • 145
  • 155
0

I think you are looking for String.Insert

Returns a new string in which a specified string is inserted at a specified index position in this instance.

So simply use

return name.Insert(extPos, "_180x140");

However as per your error is concerned use

return name.Substring(0, extPos) + "_180x140" + name.Substring(extPos);
Satpal
  • 132,252
  • 13
  • 159
  • 168