将字符串拆分为数组不起作用

时间:2023-01-16 21:30:59

I have a string like so 1500, 1500 and I am trying to split it into array like so:

我有一个类似于1500,1500的字符串,我试图将其拆分为数组,如下所示:

string[] PretaxArray = Pretax.Split(", ");

but I get this error:

但我得到这个错误:

The best overloaded method match for 'string.Split(params char[])' has some invalid arguments

'string.Split(params char [])'的最佳重载方法匹配有一些无效的参数

what am I doing wrong ?

我究竟做错了什么 ?

2 个解决方案

#1


You should try this:

你应该试试这个:

string[] PretaxArray = Pretax.Split(',');

In the Split method we usually pass a character or an array of characters and not a string.

在Split方法中,我们通常传递一个字符或一个字符数组而不是字符串。

Actually, in order to be more precise, you could pass an array of strings. But this is not your case - I assume that from the code you have posted.

实际上,为了更精确,您可以传递一个字符串数组。但这不是你的情况 - 我假设你发布的代码。

You could take a look here, where all the overloaded versions of Split method are aggregated.

您可以在这里查看,汇总所有重载版本的Split方法。

#2


String.Split has another overload

String.Split有另一个重载

string Pretax = "1500, 1500";
string[] PretaxArray = Pretax.Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries);

#1


You should try this:

你应该试试这个:

string[] PretaxArray = Pretax.Split(',');

In the Split method we usually pass a character or an array of characters and not a string.

在Split方法中,我们通常传递一个字符或一个字符数组而不是字符串。

Actually, in order to be more precise, you could pass an array of strings. But this is not your case - I assume that from the code you have posted.

实际上,为了更精确,您可以传递一个字符串数组。但这不是你的情况 - 我假设你发布的代码。

You could take a look here, where all the overloaded versions of Split method are aggregated.

您可以在这里查看,汇总所有重载版本的Split方法。

#2


String.Split has another overload

String.Split有另一个重载

string Pretax = "1500, 1500";
string[] PretaxArray = Pretax.Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries);