拆分字符串时出错

本文关键字:出错 字符串 拆分 | 更新日期: 2023-09-27 17:59:00

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

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和Hostpoints在我的bet类中声明为int。

拆分字符串时出错

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

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

您可以使用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)