Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 拆分字符串时出现混淆错误_C#_Asp.net Mvc 3_Split - Fatal编程技术网

C# 拆分字符串时出现混淆错误

C# 拆分字符串时出现混淆错误,c#,asp.net-mvc-3,split,C#,Asp.net Mvc 3,Split,我有一行代码: string[]id=Request.Params[“service”].Split(“,”) Request.Params[“service”]中的值是:“1,2” 为什么我会得到: Error 1 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments Error 2 Argument 1: cannot convert from

我有一行代码:

string[]id=Request.Params[“service”].Split(“,”)

Request.Params[“service”]
中的值是:
“1,2”

为什么我会得到:

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[]'
这对我来说毫无意义


该错误发生在等号右侧的所有对象上,您必须使用以下重载:


您需要传递一个字符(
System.Char
),而不是
字符串

string[] ids = Request.Params["service"].Split(',');
没有重载来获取
参数字符串[]
或单个
字符串,这是使代码正常工作所必需的

如果要使用一个字符串(或多个字符串)拆分,则需要使用
string[]
并指定拆分选项:

string[] ids = Request.Params["service"].Split(new[]{","}, StringSplitOptions.None);
排队
Request.Params[“service”].Split(“,”)

您正在按“
”、“
而不是“
”、“


.Split()
方法采用字符数组,而不是字符串

,正如这里提供的(“,”)所述。双引号表示字符串,Split函数接受字符数组或字符[]。使用(','),单引号表示一个字符。您还可以传递StringSplitOptions,如果您碰巧在字符串[]中获得空值,则需要传递char[],如下所示

        string splitMe = "test1,test2,";
        string[] splitted1 = splitMe.Split(',');
        string[] splitted2 = splitMe.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
        //Will be length 3 due to extra comma
        MessageBox.Show(splitted1.Length.ToString());
        //Will be length 2, Removed the empty entry since there was nothing after the comma
        MessageBox.Show(splitted2.Length.ToString());

split将一个字符数组或一个字符而不是字符串作为其参数split期望一个字符so',而不是”,“字符串[]中有一个字符,但由于他只使用一个字符,所以这并不重要+1.@ofstream这就是为什么我明确提到
params string[]
,而不是
string[]
——尽管
string[]
除了数组之外还需要
StringSplitOptions
        string splitMe = "test1,test2,";
        string[] splitted1 = splitMe.Split(',');
        string[] splitted2 = splitMe.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
        //Will be length 3 due to extra comma
        MessageBox.Show(splitted1.Length.ToString());
        //Will be length 2, Removed the empty entry since there was nothing after the comma
        MessageBox.Show(splitted2.Length.ToString());