本文介绍了索引和长度必须引用字符串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
推荐答案
我认为您正在寻找 String.Insert
I think you are looking for String.Insert
因此只需使用
return name.Insert(extPos, "_180x140");
但是根据您的错误使用
return name.Substring(0, extPos) + "_180x140" + name.Substring(extPos);
这篇关于索引和长度必须引用字符串ASP.NET中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!