索引和长度必须引用字符串ASP.NET中的位置 [英] Index and length must refer to a location within the string ASP.NET

查看:51
本文介绍了索引和长度必须引用字符串ASP.NET中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有方法

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;
    }

并查看

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

名称就像这样"http://www.ua/Content/images/ik-main-page/Catalog.png" 我需要这个 http://www.ua/Content/images/ik-main-page/Catalog_180x140.png

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

推荐答案

我认为您正在寻找

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.

因此只需使用

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

但是根据您的错误使用

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

这篇关于索引和长度必须引用字符串ASP.NET中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆