String.Split does not remove split text, only the first letter

What's going on here?

issue.CallAction has the following meaning:

 "Blah blah - WebSite - 9/20/2017 - Containers remaining changed to 0." 

Division into it, for example:

 issue.CallAction.Split("Containers remaining changed to ").ToList() 

returns more than 1 string element (as expected in this case), but the elements:

  • Blah blah - WebSite - 9/20/2017 -
  • ontainers remaining changed to 0.

I realized that Split replaces the whole line that you split, but it replaces only the first letter.

What gives?

Here is a screenshot of the compiler, happy with the line separator. Note the absence of angry red flexion:

Compiler screenshot happily resolving line separator :

+3
source share
3 answers

It looks like it treats the delimiter as Char . At least in Visual Studio 2017 / .NET 4.5, the C # that I am using, as well as in the current version, is not

 string Split(string separator) 

so you need to create an array of delimiters and populate the first.

Try the following:

  string s = "Blah blah - WebSite - 9/20/2017 - Containers remaining changed to 0."; string[] separator = { "Containers remaining changed to " }; string[] splitted = s.Split(separator, StringSplitOptions.None); 

EDIT:

I did not use VB after a few years, so I had to relearn a little here. The output from this sample program tells me that VB silently pushes String to Char when Char is called for:

 Sub PrintAChar(c As Char) Console.WriteLine("Printing your character: '" + c + "'") End Sub Sub Main() PrintAChar("B") ' Prints 'B' PrintAChar("Bob Was Here.") ' Also prints 'B' End Sub 

See @Heinzi's important comment below for best practice.

+3
source share

To get a compilation error, you can enable Option Strict On .

To split by line in VB.Net, you can use Microsoft.VisualBasic.Strings.Split :

 Dim result = Split(issue.CallAction, "Containers remaining changed to ") 
+3
source share

Is it interesting how the same code can be both valid C # and VB.NET, but at the same time give completely different results? I noticed that there was no end semicolon, but suggested that it was just an artifact of copy and paste.

Using the following code ...

 Dim text As String = "Blah blah - WebSite - 9/20/2017 - Containers remaining changed to 0." Dim list1 As List(Of String) = text.Split("Containers remaining changed to ").ToList() 

... IntelliSense indicates what causes the String.Split(ParamArray() separator as Char()) overload ( MSDN documentation includes the ParamArray modifier) ​​...

VB.NET IntelliSense for a String Passed to the String.Split Method

After executing, list1 then contains the following:

 list1(0) = "Blah blah - WebSite - 9/20/2017 - " list1(1) = "ontainers remaining changed to 0." 

If you write it using this seemingly equivalent syntax ...

 Dim list2 As List(Of String) = text.Split("C", "o", "n", "t", "a", "i", "n", "e", "r", "s", " ", "r", "e", "m", "a", "i", "n", "i", "n", "g", " ", "c", "h", "a", "n", "g", "e", "d", " ", "t", "o", " ").ToList() Dim list3 As List(Of String) = text.Split("Containers remaining changed to ".ToCharArray()).ToList() 

... IntelliSense indicates that both .Split() calls allow the same overload as above, but the results for list2 very different ...

 list2(0) = "Bl" list2(1) = "" list2(2) = "" list2(3) = "bl" list2(4) = "" list2(5) = "" list2(6) = "-" list2(7) = "W" list2(8) = "bS" list2(9) = "" list2(10) = "" list2(11) = "" list2(12) = "-" list2(13) = "9/20/2017" list2(14) = "-" list2(15) = "" list2(16) = "" list2(17) = "" list2(18) = "" list2(19) = "" list2(20) = "" list2(21) = "" list2(22) = "" list2(23) = "" list2(24) = "" list2(25) = "" list2(26) = "" list2(27) = "" list2(28) = "" list2(29) = "" list2(30) = "" list2(31) = "" list2(32) = "" list2(33) = "" list2(34) = "" list2(35) = "" list2(36) = "" list2(37) = "" list2(38) = "" list2(39) = "" list2(40) = "" list2(41) = "" list2(42) = "" list2(43) = "" list2(44) = "" list2(45) = "" list2(46) = "" list2(47) = "0." 

... and the same for list3 . In fact, we expected list1 be all the time, since it would split into any of the characters in the passed String .

Why is this happening? I find this interesting, although I am not a VB.NET guy, so maybe this is a common knowledge / trap. If we look at what the compiler produces , we see the following:

 Dim text As String = "Blah blah - WebSite - 9/20/2017 - Containers remaining changed to 0." Dim arg_13_0 As String = text Dim expr_0E As Char() = New Char(0) {} expr_0E(0) = "C"c Dim list As List(Of String) = arg_13_0.Split(expr_0E).ToList(Of String)() Dim arg_31_0 As String = text Dim expr_26 As Char() = New Char(31) {} RuntimeHelpers.InitializeArray(expr_26, fieldof(<PrivateImplementationDetails>.B46FB904AE74F7001A264BB77882D707B0E9ECC7).FieldHandle) Dim list2 As List(Of String) = arg_31_0.Split(expr_26).ToList(Of String)() Dim list3 As List(Of String) = text.Split("Containers remaining changed to ".ToCharArray()).ToList(Of String)() 

That is, in the case of list2 it passes a Char array consisting of all Char to "Containers remaining changed to " , but in the case of list1 it passes a Char array consisting of only the first Char , "C"c ! Talk about the different results of a similar code ...

I can not find a single document that connects all these points, but basically String will be implicitly converted to Char() , which calls the CChar conversion function , which returns only the first Char . Thus, the following four lines are equivalent:

 Dim list1a As List(Of String) = text.Split("Containers remaining changed to ").ToList() Dim list1b As List(Of String) = text.Split(CChar("Containers remaining changed to ")).ToList() Dim list1c As List(Of String) = text.Split("C").ToList() Dim list1d As List(Of String) = text.Split("C"c).ToList() 
+2
source share

All Articles