如何将String.Split与String分隔符一起使用?

时间:2022-09-10 22:31:21

I have string like so:

我有这样的字符串:

"ABCDEF--Split--GHIKLMO--Split--PQRSTUVWXYZ"

what I am trying to do is split this string with --Split-- being the delimiter, I have obviously tried the following:

我想要做的是用--Split--分隔这个字符串作为分隔符,我显然尝试了以下内容:

var array = item.split('--Split--');

but I get this error:

但我得到这个错误:

Too many characters in character literal

字符文字中的字符太多

Is there away to do this?

有没有办法做到这一点?

2 个解决方案

#1


The problem with your above code is that you are trying to pass in a string as a char instead of using the correct overload:

您的上述代码的问题是您尝试将字符串作为char传递而不是使用正确的重载:

item.Split(new []{"--Split--"}, StringSplitOptions.None)

MSDN Documentation

#2


You're using the string.Split that uses CHAR as parameter. See that single quote are used to create char type in C#. You have to use the overload that receives an string array as parameter.

您正在使用使用CHAR作为参数的string.Split。请参阅单引号用于在C#中创建char类型。您必须使用接收字符串数组的重载作为参数。

  item.split(new string[]{"--Split--"},StringSplitOptions.RemoveEmptyEntries);

The first parameter is an array of strings that you want to split. The second parameter, the StringSplitOptions has two possible values:

第一个参数是要拆分的字符串数组。第二个参数StringSplitOptions有两个可能的值:

  • None (will split also empty values), the example below will give you an string array with three elements. The first will be "This is an string test", second wil lbe "I will split by ", and third will be "":

    无(将分割空值),下面的示例将为您提供一个包含三个元素的字符串数组。第一个将是“这是一个字符串测试”,第二个将是“我将拆分”,第三个将是“”:

    var test = "This is an string test, I will split by ,"; var splittedStrings = test.Split(",", StringSplitOptions.None);

    var test =“这是一个字符串测试,我会分开,”; var splittedStrings = test.Split(“,”,StringSplitOptions.None);

  • RemoveEmptyEntries, this will remove the empty entries of splitted string. If you take the example above using the RemoveEmptyEntries you will have an array with only two values: "This is an string test" and "I will split by ". The empty string at final will be cutted of.

    RemoveEmptyEntries,这将删除splitted字符串的空条目。如果你使用RemoveEmptyEntries采用上面的例子,你将得到一个只有两个值的数组:“这是一个字符串测试”和“我将拆分”。最后的空字符串将被剪切掉。

#1


The problem with your above code is that you are trying to pass in a string as a char instead of using the correct overload:

您的上述代码的问题是您尝试将字符串作为char传递而不是使用正确的重载:

item.Split(new []{"--Split--"}, StringSplitOptions.None)

MSDN Documentation

#2


You're using the string.Split that uses CHAR as parameter. See that single quote are used to create char type in C#. You have to use the overload that receives an string array as parameter.

您正在使用使用CHAR作为参数的string.Split。请参阅单引号用于在C#中创建char类型。您必须使用接收字符串数组的重载作为参数。

  item.split(new string[]{"--Split--"},StringSplitOptions.RemoveEmptyEntries);

The first parameter is an array of strings that you want to split. The second parameter, the StringSplitOptions has two possible values:

第一个参数是要拆分的字符串数组。第二个参数StringSplitOptions有两个可能的值:

  • None (will split also empty values), the example below will give you an string array with three elements. The first will be "This is an string test", second wil lbe "I will split by ", and third will be "":

    无(将分割空值),下面的示例将为您提供一个包含三个元素的字符串数组。第一个将是“这是一个字符串测试”,第二个将是“我将拆分”,第三个将是“”:

    var test = "This is an string test, I will split by ,"; var splittedStrings = test.Split(",", StringSplitOptions.None);

    var test =“这是一个字符串测试,我会分开,”; var splittedStrings = test.Split(“,”,StringSplitOptions.None);

  • RemoveEmptyEntries, this will remove the empty entries of splitted string. If you take the example above using the RemoveEmptyEntries you will have an array with only two values: "This is an string test" and "I will split by ". The empty string at final will be cutted of.

    RemoveEmptyEntries,这将删除splitted字符串的空条目。如果你使用RemoveEmptyEntries采用上面的例子,你将得到一个只有两个值的数组:“这是一个字符串测试”和“我将拆分”。最后的空字符串将被剪切掉。