编译器抱怨下面的s.Split(delim)语句 [英] compiler is complaining about the s.Split(delim) statement below

查看:86
本文介绍了编译器抱怨下面的s.Split(delim)语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中每一行的项目都由"|"定界.
我正在将每一行读入字符串列表.然后处理每一行.
如您所见,这非常简单.
当编译器看到s.Split(delim);时它说:
" string.Split(parms char [])的最佳重载方法匹配具有一些无效的参数"
我不明白为什么错误存在或如何解决.
请协助更正以下代码.
谢谢

I have a text file where items on each line are delimited by "|".
I''m reading each line into a List of strings. Then processing each line.
As you can see this is pretty straight forward.
when the compiler see s.Split(delim); it says:
"The best overloaded method match for ''string.Split(parms char[])'' has some invalid arguments"
I do not understand why the error exists or how to fix it.
Please assist with a correction of the code below.
Thanks

List<string> TempList = new List<string>();

using (StreamReader r = new StreamReader(Filename))
{
    string line;
    while ((line = r.ReadLine()) != null)
    {
        TempList.Add(line);
    }
}

string delim = "|"
string[] ControlParts;
foreach ( string s in TempList) {
    ControlParts = s.Split(delim);
    _MeditechConnections.Add(ControlParts[0]);
}

推荐答案

更改
string delim = "|"




to

string delim = '|'


因此您传递的是char而不是string.

带有字符串参数的Split版本需要其他参数.


so that you are passing a char not a string.

The versions of Split that take a string parameter require additional parameters.


您始终可以使用String.ToCharArray():
You could always use the String.ToCharArray():
ControlParts = s.Split(delim.ToCharArray());


这篇关于编译器抱怨下面的s.Split(delim)语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆