var myNew = myRegex1.Split(strTargetString);
if ((myNew[1].Trim()).Length > 12)
{
myNew[1]= (myNew[1].Substring(0, 9))+"..";
} }
_model.Add(new ScoresModel()
{
NameTeam1 = (myNew[1].Trim()),
ScoreTeam1 = Convert.ToInt32(myNew[2].Trim()),
NameTeam2 = (myNew[3].Trim()),
ScoreTeam2 = Convert.ToInt32(myNew[4].Trim()),
Round = (myNew[5].Trim()).Substring(1, myNew[5].Length - 2),
URL = item.ItemLink
});
以上是我的规格。将字符串分成5个部分后。我想对第一个分割的字符串应用一个检查是否长度大于12.如果是,则将其修剪为10个字符并在字符串后面添加“..”(两个点)。
*但在修剪代码后,它在myNew [1],myNew [2],myNew [3],myNew [4],myNew [5]中给出了以下方法的错误。
*错误为The name myNew does not exist in current context.
_model.Add(new ScoresModel()
{
NameTeam1 = (myNew[1].Trim()),
ScoreTeam1 = Convert.ToInt32(myNew[2].Trim()),
NameTeam2 = (myNew[3].Trim()),
ScoreTeam2 = Convert.ToInt32(myNew[4].Trim()),
Round = (myNew[5].Trim()).Substring(1, myNew[5].Length - 2),
URL = item.ItemLink
});
答案 0 :(得分:0)
我想你想要这个:
var myNew = myRegex1.Split(strTargetString);
if ((myNew[0].Trim()).Length > 12)
{
myNew[0]= myNew[0].Substring(0, 10)+"..";
}
_model.Add(new ScoresModel()
{
NameTeam1 = (myNew[0].Trim()),
ScoreTeam1 = Convert.ToInt32(myNew[1].Trim()),
NameTeam2 = (myNew[2].Trim()),
ScoreTeam2 = Convert.ToInt32(myNew[3].Trim()),
Round = (myNew[4].Trim()).Substring(1, myNew[4].Length - 2),
URL = item.ItemLink
});
答案 1 :(得分:0)
试试这个:
var myNew = myRegex1.Split(strTargetString);
_model.Add(new ScoresModel()
{
NameTeam1 = myNew[0].Trim().Length > 12 ? myNew[0].Trim().Substring(0, 9)+".." : myNew[0].Trim(),
ScoreTeam1 = Convert.ToInt32(myNew[1].Trim()),
NameTeam2 = myNew[2].Trim(),
ScoreTeam2 = Convert.ToInt32(myNew[3].Trim()),
Round = myNew[4].Trim().Substring(1, myNew[4].Trim().Length - 2),
URL = item.ItemLink
});
拆分结果为0索引;)像这样,你不设置数组的第一个元素,只需使用它。
使用Trim测试字符串时要小心,如果以后不使用它(对于子字符串),如果字符串以空格开头,则可能会出错。