拆分字符串时出现令人困惑的错误 [英] Confusing error when splitting a string
本文介绍了拆分字符串时出现令人困惑的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这行代码:
string[] ids = 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[]'
这对我来说毫无意义....
This makes no sense to me....
错误发生在等号右边的所有内容
The error happens on everything to the right of the equal sign
推荐答案
你需要传递一个字符(System.Char
),而不是一个string
:
You need to pass a character (System.Char
), not a string
:
string[] ids = Request.Params["service"].Split(',');
String.Split
<没有过载/a> 接受一个 params string[]
或单个 string
,这是使您的代码工作所必需的.
There is no overload to String.Split
that takes a params string[]
or a single string
, which is what would be required to make your code work.
如果你想用一个字符串(或多个字符串)分割,你需要使用 string[]
并指定分割选项:
If you wanted to split with a string (or multiple strings), you would need to use a string[]
and specify splitting options:
string[] ids = Request.Params["service"].Split(new[]{","}, StringSplitOptions.None);
这篇关于拆分字符串时出现令人困惑的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文