0
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
                    });
4

2 回答 2

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-indexed ;) 像这样,您不设置数组的第一个元素,只需使用它即可。

使用 Trim 测试字符串时要小心,如果以后不使用它(用于子字符串),如果字符串以空格开头,则可能会出错。

于 2013-03-09T17:59:50.137 回答
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
                    });
于 2013-03-09T17:51:53.890 回答