【发布时间】:2018-05-11 04:21:21
【问题描述】:
为什么这不起作用? 例如,如果 teaserLength 返回 300,则为 true。 如果它返回例如 37,它是假的..即使它应该被反转......
我的代码:
@{
int teaserLength = item.TeaserText.Length;
}
@if (teaserLength >= 75)
{
@item.TeaserText
}
else {
@item.TeaserText.Substring(1, 75)
}
以及为什么一个长度为37的TeaserText,给一个
ArgumentOutOfRangeException
Index and length must refer to a location within the string.
在子字符串上?
【问题讨论】:
-
因为你说的是
if the teaserLength is smaller than 75, take the substring? -
子字符串从索引 0 开始。使用数组的方式相同。
-
@IanH。我不是说,如果 teaserText 小于或等于 75,请使用 Teaser 文本。 else if large use substring ?
-
您是说如果 teaserText 小于 75,则从索引 1(即第二个特点)。你想要它做什么?
-
您可以完全摆脱
teaserLength变量和if/else并返回@item.TeaserText.Substring(0, Math.Min(item.TeaserText.Length, 75));
标签: c# substring string-length