分割字符串

时间:2016-07-28 10:38:11

标签: c# string split

当我尝试拆分一些字符串时,我遇到了两种错误:

Error   1   The best overloaded method match for 'string.Split(params char[])' has some invalid arguments
Error   2   Argument 1: cannot convert from 'string' to 'char[]'

这是我的代码部分:

if (string.IsNullOrEmpty(data_odd))
{
    if (node.GetAttributeValue("class", "").Contains(("first-cell")))
        rowBet.Match = node.InnerText.Trim();
    var matchTeam = rowBet.Match.Split("-", StringSplitOptions.RemoveEmptyEntries);
    rowBet.Home = matchTeam[0];
    rowBet.Host = matchTeam[1];

    if (node.GetAttributeValue("class", "").Contains(("result")))
        rowBet.Result = node.InnerText.Trim();
    var matchPoints = rowBet.Result.Split(":", StringSplitOptions.RemoveEmptyEntries);
    rowBet.HomePoints = int.Parse(matchPoints[0];
    rowBet.HostPoints = int.Parse(matchPoints[1];

    if (node.GetAttributeValue("class", "").Contains(("last-cell")))
        rowBet.Date = node.InnerText.Trim();
}

我真的不知道如何解决它。我希望你能帮助我。

编辑:Homepoints和Hostpoint在我的投注类中声明为int。

2 个答案:

答案 0 :(得分:4)

你必须将分隔符作为字符传递,而不是作为字符串传递(带单引号)

var matchTeam = rowBet.Match.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

答案 1 :(得分:1)

您可以使用ToCharArray()

将字符串转换为char[]
var matchTeam = rowBet.Match.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var matchPoints = rowBet.Result.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

或者即时生成阵列!

var matchTeam = rowBet.Match.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
var matchPoints = rowBet.Result.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)